|
| 1 | +## How To Implement Helper Functions In Handlebars |
| 2 | + |
| 3 | +#### Registering a Handlebars helper |
| 4 | +```js |
| 5 | +var Handlebars = require('handlebars'); |
| 6 | + |
| 7 | +Handlebars.registerHelper('link', function(name, age) { |
| 8 | + // do something with the parameters |
| 9 | + var newString = name + ' is ' + age; |
| 10 | + return newString; |
| 11 | +}); |
| 12 | +``` |
| 13 | +- Here the helper function is called `link` |
| 14 | +- It takes two parameters (`name` and `age`), does something with them, and then returns something |
| 15 | + |
| 16 | +#### Calling a helper within a `.hbs` file |
| 17 | +```html |
| 18 | +<article> |
| 19 | + {{link name age}} |
| 20 | +</article> |
| 21 | +``` |
| 22 | +- Here `link` is the name of the Handlebars helper, and `name` and `age` are parameters passed to the helper - you could think of it like `link(name, age)` |
| 23 | + |
| 24 | +#### Parameters: Strings, Numbers, Booleans |
| 25 | +- In the above examples, `name` and `age` are key-value pairs that are passed to the template as an object, where they can be accessed and passed to the helper function: |
| 26 | +```js |
| 27 | +var context = {name: 'caesar', age: 55}; |
| 28 | +var html = template(context); |
| 29 | +``` |
| 30 | +```html |
| 31 | +<!-- html / hbs template file --> |
| 32 | +{{link name age}} <!-- caesar is 55 --> |
| 33 | +``` |
| 34 | + |
| 35 | +- You can instead pass strings, numbers and booleans as parameters |
| 36 | +```html |
| 37 | +<!-- html / hbs template file --> |
| 38 | +{{link "augustus", 90}} <!-- augustus is 90 --> |
| 39 | +``` |
| 40 | + |
| 41 | +#### Hash Arguments |
| 42 | +- Helpers can receive an optional sequence of key-value pairs as their **final parameter**, referred to as _hash arguments_ |
| 43 | +- These key-value pairs can be accessed within the helper function from the `hash` property of the helper's final parameter |
| 44 | + |
| 45 | +```html |
| 46 | +<!-- html / hbs template file --> |
| 47 | +{{link name age food="hummus" address="naz"}} |
| 48 | +``` |
| 49 | +```js |
| 50 | +var Handlebars = require('handlebars'); |
| 51 | + |
| 52 | +Handlebars.registerHelper('link', function(name, age, options) { |
| 53 | + console.log(options.hash); // {food: 'hummus', address: 'naz'} |
| 54 | +}); |
| 55 | +``` |
| 56 | + |
| 57 | +- This could be useful when creating an html element with parameters: |
| 58 | + |
| 59 | +```html |
| 60 | +<!-- html / hbs template file --> |
| 61 | +{{link "See more..." href="story.url" class="story"}} |
| 62 | +``` |
| 63 | +```js |
| 64 | +Handlebars.registerHelper('link', function(text, options) { |
| 65 | + var attrs = []; |
| 66 | + |
| 67 | + for (var prop in options.hash) { |
| 68 | + attrs.push(prop + '="' + options.hash[prop] + '"'); |
| 69 | + } |
| 70 | + |
| 71 | + return "<a " + attrs.join(" ") + ">" + text + "</a>"; |
| 72 | + // <a href="story.url" class="story">See more...</a> |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +#### References |
| 77 | +- http://handlebarsjs.com/expressions.html |
| 78 | +- [Example a la Mavis](https://github.com/m4v15/templates) |
0 commit comments