Skip to content

Instantly share code, notes, and snippets.

@robby
Last active August 29, 2015 14:05
Show Gist options
  • Save robby/b7dd08341fb781f0cacc to your computer and use it in GitHub Desktop.
Save robby/b7dd08341fb781f0cacc to your computer and use it in GitHub Desktop.
handlebars script/style helpers
<!DOCTYPE html>
<html>
<head>
<title>Robby</title>
{{#each styles}}
<link rel="stylesheet" href="{{filename}}" />{{/each}}
{{#each styleLiterals}}
<style>
{{{this}}}
</style>
{{/each}}
</head>
<body>
<header>Hi</header>
<section>
{{{ content }}}
</section>
{{#each scripts}}
<script src="{{filename}}" defer></script>
{{/each}}
{{#each scriptLiterals}}
<script>
{{{this}}}
</script>
{{/each}}
</body>
</html>
{{script "robby.js"}}
{{style "robbyStyle.css"}}
{{#script}}
alert('hi');
{{/script}}
{{#style}}
body { background-color: #0f0; }
{{/style}}
hbs.helpers.script = function(context) {
context && context.fn && this.scriptLiterals.push(context.fn());
context && !context.fn && this.scripts.push({ filename: context });
return '';
};
hbs.helpers.style = function(context) {
context && context.fn && this.styleLiterals.push(context.fn());
context && !context.fn && this.styles.push({ filename: context });
return '';
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment