Skip to content

Instantly share code, notes, and snippets.

@rgrwkmn
Created November 12, 2013 19:35
Show Gist options
  • Select an option

  • Save rgrwkmn/7437275 to your computer and use it in GitHub Desktop.

Select an option

Save rgrwkmn/7437275 to your computer and use it in GitHub Desktop.
A Smarty function for including RequireJS and AMD modules in an established (read: antiquated) PHP project. It doesn't make sense to add require into everything because only new functionality and iterative enhancements will be able to make use of it.
<?php
global $requirejsDeps;
$requirejsDeps = array();
function smarty_function_requirejs($params, &$smarty)
{
global $requirejsDeps;
// if finishing and something has been required, do it
if ($params['finish']) {
if (count($requirejsDeps)) {
$data['deps'] = implode("','", $requirejsDeps);
return $smarty->fetch('includes/require-js.tpl', $data);
} else {
return;
}
}
if ($params['deps']) {
$requirejsDeps = array_merge($requirejsDeps, $params['deps']);
}
}
{literal}
<script>
require = {
baseUrl: '/public/js',
paths: {
}
}
</script>
<script src="/public/js/vendor/require.min.js"></script>
<script>
// jQuery already defined on the page
define('jquery',function(){return jQuery});
require(['{/literal}{$deps}{literal}']);</script>
{/literal}
<h1>Hello World</h1>
{requirejs deps=array('myFirstModule', 'mySecondModule')}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment