Skip to content

Instantly share code, notes, and snippets.

@johndhancock
Last active December 21, 2018 19:56
Show Gist options
  • Save johndhancock/38b9bdcda9a4490b90a66a6ef504ccd0 to your computer and use it in GitHub Desktop.
Save johndhancock/38b9bdcda9a4490b90a66a6ef504ccd0 to your computer and use it in GitHub Desktop.
sample handlebar loop and helper function
<!-- this small piece of html would be inside a larger template that's being used to draw something, such as a post -->
<ul>
{{#each pills}} <!-- your each function called on an array that's part of your data object -->
<li>{{getPillText this}}</li> <!-- passing the value of your array to a helper function (first argument in this template tag -->
{{#/each}}
</ul>
// an example of your glossary
const myPills = [
{
pillNo: 3,
pillText: 'No Guns',
},
];
// helper function gets two parameters: name of function (getPillText), and the value being passed in from template (id)
Handlebars.registerHelper('getPillText', (id) => {
myPills.find((pill) => {
if (pill.pillNo === id) {
return pill.pillText;
} return 'No text given';
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment