Created
November 12, 2013 19:35
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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']); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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