Last active
December 21, 2018 19:56
-
-
Save johndhancock/38b9bdcda9a4490b90a66a6ef504ccd0 to your computer and use it in GitHub Desktop.
sample handlebar loop and helper function
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
<!-- 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> |
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
// 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