1 | 1 | View Code Duplication | var handlebars = require('handlebars'); |
2 | /** |
||
3 | * Generates a Handlebars block helper called #unlesspage for use in templates. This helper must be re-generated for every page that's rendered, because the return value of the function is dependent on the name of the current page. |
||
4 | * @param {string} pageName - Name of the page to use in the helper function. |
||
5 | * @returns {function} A Handlebars helper function. |
||
6 | */ |
||
7 | function unlessPage (pageName) { |
||
8 | /** |
||
9 | * Handlebars block helper that renders the content inside of it based on the current page. |
||
10 | * @param {string...} pages - One or more pages to check. |
||
11 | * @param (object) options - Handlebars object. |
||
12 | * @example |
||
13 | * {{#unlesspage 'index', 'about'}}This must NOT be the index or about page.{{/unlesspage}} |
||
14 | * @return The content inside the helper if no page matches, or an empty string if a page does match. |
||
15 | */ |
||
16 | 25 | return function() { |
|
17 | 4 | var params = Array.prototype.slice.call(arguments); |
|
18 | 4 | var pages = params.slice(0, -1); |
|
19 | 4 | var options = params[params.length - 1]; |
|
20 | |||
21 | 4 | for (var i in pages) { |
|
0 ignored issues
–
show
|
|||
22 | 4 | if (pages[i] === pageName) { |
|
23 | 2 | return ''; |
|
24 | } |
||
25 | } |
||
26 | |||
27 | 2 | return options.fn(this); |
|
28 | } |
||
29 | } |
||
30 | |||
31 | 1 | module.exports = unlessPage; |
|
32 | handlebars.registerHelper('unlessPage', unlessPage); |
||
33 | handlebars.registerHelper('unlesspage', unlessPage); |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: