Skip to content

Instantly share code, notes, and snippets.

@jeffturcotte
Last active December 27, 2016 12:03
Show Gist options
  • Save jeffturcotte/9509721 to your computer and use it in GitHub Desktop.
Save jeffturcotte/9509721 to your computer and use it in GitHub Desktop.
RequireJS per page module
require.config({
baseUrl: '/scripts/lib',
paths: {
app: '../app',
jquery: 'jquery-1.11.0',
handlebars: 'handlebars-v1.3.0',
ckeditorCore: 'ckeditor/ckeditor',
ckeditor: 'ckeditor/adapters/jquery'
},
shim: {
ckeditor: {
deps: ['jquery', 'ckeditorCore']
},
handlebars: {
exports: 'Handlebars'
}
}
});
// this following snippet reads the data-module attribute and requires it
require(['jquery'], function($) {
if (module = $('script[src$="require.js"]').data('module')) {
require([module]);
}
});
<!doctype html>
<html>
<head>
<title>{{ title|title }}</title>
<link rel="stylesheet" href="/styles/common.css" />
<link rel="stylesheet" href="/styles/layout.css" />
</head>
<body>
{{ include('header.html') }}
{% block content %}
{% endblock %}
{{ include('footer.html') }}
{# we set the script variable wherever we are rendering the template #}
{# and twig adds it to the data-module attr below #}
<script
data-module="{{ script }}"
data-main="/scripts/app.js"
src="/scripts/require.js">
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment