Code injection into custom template using handlebars

Hi you can do that, I have not done it but there is similar thing already in default casper theme.

In casper’s default.hbs file near the bottom you will see following code with comment. don’t miss the triple curly braces.

{{!-- The #block helper will pull in data from the #contentFor other template files. In this case, there's some JavaScript which we only want to use in post.hbs, but it needs to be included down here, after jQuery has already loaded. --}}

{{{block "scripts"}}}

Then see post.hbs file and you will find following code structure.

{{#contentFor "scripts"}}
<script>
// your scripts goes here ...
// this can be used for not only scripts but for any other code block.
</script>
{{/contentFor}}

So, put the {{{block}}} helper in default.hbs file where you want to load the inline script or external script.

Then in your custom template file, wrap that script using {{contentFor}} helper. Make sure the contentFor name ( in example above “scripts” ) match with block name.

3 Likes