Issues (117)

lib/handlebars/ifPage.js (1 issue)

1 1 View Code Duplication
var handlebars = require('handlebars');
2
/**
3
 * Generates a Handlebars block helper called #ifpage 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 ifPage (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
   * {{#ifpage 'index' 'about'}}This must be the index or about page.{{/ifpage}}
14
   * @return The content inside the helper if a page matches, or an empty string if not.
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
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

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:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
22 4
      if (pages[i] === pageName) {
23 2
        return options.fn(this);
24
      }
25
    }
26
27 2
    return '';
28
  }
29
}
30
31 1
module.exports = ifPage;
32
handlebars.registerHelper('ifPage', ifPage);
33
handlebars.registerHelper('ifpage', ifPage);