├── .gitattributes ├── .verb.md ├── index.js ├── .travis.yml ├── .editorconfig ├── .gitignore ├── lib ├── helpers │ ├── index.js │ ├── log.js │ ├── unless.js │ ├── lookup.js │ ├── with.js │ ├── if.js │ └── each.js └── utils.js ├── LICENSE ├── package.json ├── README.md ├── .eslintrc.json └── test.js /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary 11 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | > Register the helpers with an assemble instance called `app`. 4 | 5 | ```js 6 | var assemble = require('assemble'); 7 | var app = assemble(); 8 | app.helpers(require('{%= name %}')); 9 | ``` 10 | ## Helpers 11 | {%= apidocs('./lib/helpers/*.js') %} 12 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * assemble-handlebars-helpers 3 | * 4 | * Copyright (c) 2016, Brian Woodward. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | module.exports = require('./lib/helpers'); 11 | 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '7' 9 | - '6' 10 | - '5' 11 | - '4' 12 | - '0.12' 13 | - '0.10' 14 | matrix: 15 | fast_finish: true 16 | allow_failures: 17 | - node_js: "0.10" 18 | - node_js: "0.12" 19 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | end_of_line = lf 6 | charset = utf-8 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 12 | trim_trailing_whitespace = false 13 | insert_final_newline = false -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | .nyc* 10 | 11 | # npm 12 | node_modules 13 | npm-debug.log 14 | 15 | # yarn 16 | yarn.lock 17 | yarn-error.log 18 | 19 | # misc 20 | _gh_pages 21 | _draft 22 | _drafts 23 | bower_components 24 | vendor 25 | temp 26 | tmp 27 | TODO.md 28 | 29 | examples/*/dist 30 | examples/*/site 31 | -------------------------------------------------------------------------------- /lib/helpers/index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * assemble-handlebars-helpers 3 | * 4 | * The following helpers are re-implemented based on the built-in Handlebars helpers. 5 | * Changes have been made for handling assemble specifics like context and errors. 6 | * 7 | * See [https://github.com/wycats/handlebars.js/tree/master/lib/handlebars/helpers] for 8 | * original implementation. 9 | * 10 | * Copyright (c) 2016, Brian Woodward. 11 | * Licensed under the MIT License. 12 | */ 13 | 14 | 'use strict'; 15 | 16 | module.exports = require('export-files')(__dirname); 17 | -------------------------------------------------------------------------------- /lib/utils.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var utils = {}; 4 | 5 | utils.createFrame = require('create-frame'); 6 | 7 | /** 8 | * blockParams method from internal Handlebars utils 9 | */ 10 | 11 | utils.blockParams = function(params, ids) { 12 | params.path = ids; 13 | return params; 14 | }; 15 | 16 | /** 17 | * appendContextPath method from internal Handlebars utils 18 | */ 19 | 20 | utils.appendContextPath = function(contextPath, id) { 21 | return (contextPath ? contextPath + '.' : '') + id; 22 | }; 23 | 24 | utils.isEmpty = function(val) { 25 | if (!val && val !== 0) { 26 | return true; 27 | } 28 | 29 | if (Array.isArray(val) && !val.length) { 30 | return true; 31 | } 32 | 33 | return false; 34 | }; 35 | 36 | module.exports = utils; 37 | -------------------------------------------------------------------------------- /lib/helpers/log.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * assemble-handlebars-helpers 3 | * 4 | * The following helpers are re-implemented based on the built-in Handlebars helpers. 5 | * Changes have been made for handling assemble specifics like context and errors. 6 | * 7 | * See [https://github.com/wycats/handlebars.js/tree/master/lib/handlebars/helpers] for 8 | * original implementation. 9 | * 10 | * Copyright (c) 2016, Brian Woodward. 11 | * Licensed under the MIT License. 12 | */ 13 | 'use strict'; 14 | 15 | /** 16 | * ```handlebars 17 | * {{log foo}} 18 | * ``` 19 | * @name log 20 | * @api public 21 | */ 22 | 23 | module.exports = function log(/* message, options */) { 24 | console.log.apply(console, arguments); 25 | }; 26 | -------------------------------------------------------------------------------- /lib/helpers/unless.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * assemble-handlebars-helpers 3 | * 4 | * The following helpers are re-implemented based on the built-in Handlebars helpers. 5 | * Changes have been made for handling assemble specifics like context and errors. 6 | * 7 | * See [https://github.com/wycats/handlebars.js/tree/master/lib/handlebars/helpers] for 8 | * original implementation. 9 | * 10 | * Copyright (c) 2016, Brian Woodward. 11 | * Licensed under the MIT License. 12 | */ 13 | 'use strict'; 14 | 15 | var _if = require('./if'); 16 | 17 | /** 18 | * ```handlebars 19 | * {{#unless foo}} 20 | * {{foo}} 21 | * {{else}} 22 | * {{bar}} 23 | * {{/unless}} 24 | * ``` 25 | * @name unless 26 | * @api public 27 | */ 28 | 29 | module.exports = function unless(conditional, options) { 30 | return _if.call(this, conditional, {fn: options.inverse, inverse: options.fn, hash: options.hash}); 31 | }; 32 | -------------------------------------------------------------------------------- /lib/helpers/lookup.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * assemble-handlebars-helpers 3 | * 4 | * The following helpers are re-implemented based on the built-in Handlebars helpers. 5 | * Changes have been made for handling assemble specifics like context and errors. 6 | * 7 | * See [https://github.com/wycats/handlebars.js/tree/master/lib/handlebars/helpers] for 8 | * original implementation. 9 | * 10 | * Copyright (c) 2016, Brian Woodward. 11 | * Licensed under the MIT License. 12 | */ 13 | 'use strict'; 14 | 15 | var get = require('get-value'); 16 | 17 | /** 18 | * Returns a value from an object by the property name. 19 | * 20 | * ```handlebars 21 | * {{lookup foo "bar"}} 22 | * 23 | * {{! as a subexpression }} 24 | * {{#each (lookup foo "items")}} 25 | * {{this}} 26 | * {{/each}} 27 | * ``` 28 | * @name lookup 29 | * @api public 30 | */ 31 | 32 | module.exports = function lookup(obj, field) { 33 | return obj && get(obj, field); 34 | }; 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016-2017, Brian Woodward 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /lib/helpers/with.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * assemble-handlebars-helpers 3 | * 4 | * The following helpers are re-implemented based on the built-in Handlebars helpers. 5 | * Changes have been made for handling assemble specifics like context and errors. 6 | * 7 | * See [https://github.com/wycats/handlebars.js/tree/master/lib/handlebars/helpers] for 8 | * original implementation. 9 | * 10 | * Copyright (c) 2016, Brian Woodward. 11 | * Licensed under the MIT License. 12 | */ 13 | 'use strict'; 14 | 15 | var utils = require('../utils'); 16 | 17 | /** 18 | * ```handlebars 19 | * {{#with foo}} 20 | * {{bar}} 21 | * {{/with}} 22 | * ``` 23 | * @name with 24 | * @api public 25 | */ 26 | 27 | module.exports = function _with(context, options) { 28 | if (typeof context === 'function') { 29 | context = context.call(this); 30 | } 31 | 32 | var fn = options.fn; 33 | 34 | if (!utils.isEmpty(context)) { 35 | var data = options.data; 36 | if (options.data && options.ids) { 37 | data = utils.createFrame(options.data); 38 | data.contextPath = utils.appendContextPath(options.data.contextPath, options.ids[0]); 39 | } 40 | 41 | return fn(context, { 42 | data: data, 43 | blockParams: utils.blockParams([context], [data && data.contextPath]) 44 | }); 45 | } else { 46 | return options.inverse(this._parent || this); 47 | } 48 | }; 49 | -------------------------------------------------------------------------------- /lib/helpers/if.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * assemble-handlebars-helpers 3 | * 4 | * The following helpers are re-implemented based on the built-in Handlebars helpers. 5 | * Changes have been made for handling assemble specifics like context and errors. 6 | * 7 | * See [https://github.com/wycats/handlebars.js/tree/master/lib/handlebars/helpers] for 8 | * original implementation. 9 | * 10 | * Copyright (c) 2016, Brian Woodward. 11 | * Licensed under the MIT License. 12 | */ 13 | 'use strict'; 14 | 15 | var utils = require('../utils'); 16 | 17 | /** 18 | * ```handlebars 19 | * {{#if foo}} 20 | * {{foo}} 21 | * {{else}} 22 | * {{bar}} 23 | * {{/if}} 24 | * ``` 25 | * @name if 26 | * @api public 27 | */ 28 | 29 | module.exports = function _if(conditional, options) { 30 | var context = this._parent || this; 31 | if (typeof conditional === 'function') { 32 | conditional = conditional.call(this); 33 | } 34 | 35 | // Default behavior is to render the positive path if the value is truthy and not empty. 36 | // The `includeZero` option may be set to treat the condtional as purely not empty based on the 37 | // behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative. 38 | if ((!options.hash.includeZero && !conditional) || utils.isEmpty(conditional)) { 39 | return options.inverse(context); 40 | } else { 41 | return options.fn(context); 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "assemble-handlebars-helpers", 3 | "description": "Default helpers for use in assemble to replace the built-in Handlebars helpers.", 4 | "version": "0.1.3", 5 | "homepage": "https://github.com/assemble/assemble-handlebars-helpers", 6 | "author": "Brian Woodward (https://github.com/doowb)", 7 | "repository": "assemble/assemble-handlebars-helpers", 8 | "bugs": { 9 | "url": "https://github.com/assemble/assemble-handlebars-helpers/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js", 14 | "lib" 15 | ], 16 | "main": "index.js", 17 | "engines": { 18 | "node": ">=0.10.0" 19 | }, 20 | "scripts": { 21 | "test": "mocha" 22 | }, 23 | "dependencies": { 24 | "create-frame": "^1.0.0", 25 | "export-files": "^2.1.1", 26 | "get-value": "^2.0.6" 27 | }, 28 | "devDependencies": { 29 | "assemble": "^0.23.0", 30 | "capture-stream": "^0.1.2", 31 | "gulp-format-md": "^0.1.11", 32 | "mocha": "^3.2.0" 33 | }, 34 | "keywords": [ 35 | "assemble", 36 | "assemble-core", 37 | "built", 38 | "built-in", 39 | "core", 40 | "default", 41 | "each", 42 | "handlebars", 43 | "helpers", 44 | "in", 45 | "log", 46 | "lookup", 47 | "templates", 48 | "unless" 49 | ], 50 | "verb": { 51 | "plugins": [ 52 | "gulp-format-md" 53 | ], 54 | "reflinks": [ 55 | "verb" 56 | ], 57 | "toc": false, 58 | "layout": "default", 59 | "lint": { 60 | "reflinks": true 61 | }, 62 | "tasks": [ 63 | "readme" 64 | ] 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /lib/helpers/each.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * assemble-handlebars-helpers 3 | * 4 | * The following helpers are re-implemented based on the built-in Handlebars helpers. 5 | * Changes have been made for handling assemble specifics like context and errors. 6 | * 7 | * See [https://github.com/wycats/handlebars.js/tree/master/lib/handlebars/helpers] for 8 | * original implementation. 9 | * 10 | * Copyright (c) 2016, Brian Woodward. 11 | * Licensed under the MIT License. 12 | */ 13 | 'use strict'; 14 | 15 | var utils = require('../utils'); 16 | 17 | /** 18 | * Iterate over an array or object's key/value pairs. 19 | * 20 | * ```handlebars 21 | * {{#each arr}} 22 | * {{this}} 23 | * {{/each}} 24 | * ``` 25 | * @name each 26 | * @api public 27 | */ 28 | 29 | module.exports = function each(context, options) { 30 | if (!options) { 31 | throw new Error('Must pass iterator to #each'); 32 | } 33 | 34 | var fn = options.fn; 35 | var inverse = options.inverse; 36 | 37 | var i = 0; 38 | var ret = ''; 39 | var contextPath; 40 | 41 | if (options.data && options.ids) { 42 | contextPath = utils.appendContextPath(options.data.contextPath, options.ids[0]) + '.'; 43 | } 44 | 45 | if (typeof context === 'function') { 46 | context = context.call(this); 47 | } 48 | 49 | function execIteration(field, index, last) { 50 | if (options.data) { 51 | var data = utils.createFrame(options.data); 52 | data.key = field; 53 | data.index = index; 54 | data.first = index === 0; 55 | data.last = !!last; 56 | 57 | if (contextPath) { 58 | data.contextPath = contextPath + field; 59 | } 60 | } 61 | 62 | ret = ret + fn(context[field], { 63 | data: data, 64 | blockParams: utils.blockParams([context[field], field], [contextPath + field, null]) 65 | }); 66 | } 67 | 68 | if (context && typeof context === 'object') { 69 | if (Array.isArray(context)) { 70 | for (var j = context.length; i < j; i++) { 71 | if (i in context) { 72 | execIteration(i, i, i === context.length - 1); 73 | } 74 | } 75 | } else { 76 | var priorKey; 77 | 78 | for (var key in context) { 79 | if (context.hasOwnProperty(key)) { 80 | // We're running the iterations one step out of sync so we can detect 81 | // the last iteration without have to scan the object twice and create 82 | // an itermediate keys array. 83 | if (priorKey !== undefined) { 84 | execIteration(priorKey, i - 1); 85 | } 86 | priorKey = key; 87 | i++; 88 | } 89 | } 90 | if (priorKey !== undefined) { 91 | execIteration(priorKey, i - 1, true); 92 | } 93 | } 94 | } 95 | 96 | if (i === 0) { 97 | ret = inverse(this._parent || this); 98 | } 99 | 100 | return ret; 101 | }; 102 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # assemble-handlebars-helpers [![NPM version](https://img.shields.io/npm/v/assemble-handlebars-helpers.svg?style=flat)](https://www.npmjs.com/package/assemble-handlebars-helpers) [![NPM downloads](https://img.shields.io/npm/dm/assemble-handlebars-helpers.svg?style=flat)](https://npmjs.org/package/assemble-handlebars-helpers) [![Build Status](https://img.shields.io/travis/assemble/assemble-handlebars-helpers.svg?style=flat)](https://travis-ci.org/assemble/assemble-handlebars-helpers) 2 | 3 | > Default helpers for use in assemble to replace the built-in Handlebars helpers. 4 | 5 | ## Install 6 | 7 | Install with [npm](https://www.npmjs.com/): 8 | 9 | ```sh 10 | $ npm install assemble-handlebars-helpers --save 11 | ``` 12 | 13 | ## Usage 14 | 15 | > Register the helpers with an assemble instance called `app`. 16 | 17 | ```js 18 | var assemble = require('assemble'); 19 | var app = assemble(); 20 | app.helpers(require('assemble-handlebars-helpers')); 21 | ``` 22 | 23 | ## Helpers 24 | 25 | ### [each](lib/helpers/each.js#L29) 26 | 27 | Iterate over an array or object's key/value pairs. 28 | 29 | **Example** 30 | 31 | ```handlebars 32 | {{#each arr}} 33 | {{this}} 34 | {{/each}} 35 | ``` 36 | 37 | ### [if](lib/helpers/if.js#L29) 38 | 39 | **Example** 40 | 41 | ```handlebars 42 | {{#if foo}} 43 | {{foo}} 44 | {{else}} 45 | {{bar}} 46 | {{/if}} 47 | ``` 48 | 49 | ### [log](lib/helpers/log.js#L23) 50 | 51 | **Example** 52 | 53 | ```handlebars 54 | {{log foo}} 55 | ``` 56 | 57 | ### [lookup](lib/helpers/lookup.js#L32) 58 | 59 | Returns a value from an object by the property name. 60 | 61 | **Example** 62 | 63 | ```handlebars 64 | {{lookup foo "bar"}} 65 | 66 | {{! as a subexpression }} 67 | {{#each (lookup foo "items")}} 68 | {{this}} 69 | {{/each}} 70 | ``` 71 | 72 | ### [unless](lib/helpers/unless.js#L29) 73 | 74 | **Example** 75 | 76 | ```handlebars 77 | {{#unless foo}} 78 | {{foo}} 79 | {{else}} 80 | {{bar}} 81 | {{/unless}} 82 | ``` 83 | 84 | ### [with](lib/helpers/with.js#L27) 85 | 86 | **Example** 87 | 88 | ```handlebars 89 | {{#with foo}} 90 | {{bar}} 91 | {{/with}} 92 | ``` 93 | 94 | ## Contributing 95 | 96 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/assemble/assemble-handlebars-helpers/issues/new). 97 | 98 | ## Building docs 99 | 100 | Generate readme and API documentation with [verb](https://github.com/verbose/verb): 101 | 102 | ```sh 103 | $ npm install verb && npm run docs 104 | ``` 105 | 106 | Or, if [verb](https://github.com/verbose/verb) is installed globally: 107 | 108 | ```sh 109 | $ verb 110 | ``` 111 | 112 | ## Running tests 113 | 114 | Install dev dependencies: 115 | 116 | ```sh 117 | $ npm install -d && npm test 118 | ``` 119 | 120 | ## Author 121 | 122 | **Brian Woodward** 123 | 124 | * [github/doowb](https://github.com/doowb) 125 | * [twitter/doowb](http://twitter.com/doowb) 126 | 127 | ## License 128 | 129 | Copyright © 2016, [Brian Woodward](https://github.com/doowb). 130 | Released under the [MIT license](https://github.com/assemble/assemble-handlebars-helpers/blob/master/LICENSE). 131 | 132 | *** 133 | 134 | _This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on April 25, 2016._ -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | 7 | "env": { 8 | "browser": false, 9 | "es6": true, 10 | "node": true, 11 | "mocha": true 12 | }, 13 | 14 | "globals": { 15 | "document": false, 16 | "navigator": false, 17 | "window": false 18 | }, 19 | 20 | "rules": { 21 | "accessor-pairs": 2, 22 | "arrow-spacing": [2, { "before": true, "after": true }], 23 | "block-spacing": [2, "always"], 24 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 25 | "comma-dangle": [2, "never"], 26 | "comma-spacing": [2, { "before": false, "after": true }], 27 | "comma-style": [2, "last"], 28 | "constructor-super": 2, 29 | "curly": [2, "multi-line"], 30 | "dot-location": [2, "property"], 31 | "eol-last": 2, 32 | "eqeqeq": [2, "allow-null"], 33 | "generator-star-spacing": [2, { "before": true, "after": true }], 34 | "handle-callback-err": [2, "^(err|error)$" ], 35 | "indent": [2, 2, { "SwitchCase": 1 }], 36 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 37 | "keyword-spacing": [2, { "before": true, "after": true }], 38 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 39 | "new-parens": 2, 40 | "no-array-constructor": 2, 41 | "no-caller": 2, 42 | "no-class-assign": 2, 43 | "no-cond-assign": 2, 44 | "no-const-assign": 2, 45 | "no-control-regex": 2, 46 | "no-debugger": 2, 47 | "no-delete-var": 2, 48 | "no-dupe-args": 2, 49 | "no-dupe-class-members": 2, 50 | "no-dupe-keys": 2, 51 | "no-duplicate-case": 2, 52 | "no-empty-character-class": 2, 53 | "no-eval": 2, 54 | "no-ex-assign": 2, 55 | "no-extend-native": 2, 56 | "no-extra-bind": 2, 57 | "no-extra-boolean-cast": 2, 58 | "no-extra-parens": [2, "functions"], 59 | "no-fallthrough": 2, 60 | "no-floating-decimal": 2, 61 | "no-func-assign": 2, 62 | "no-implied-eval": 2, 63 | "no-inner-declarations": [2, "functions"], 64 | "no-invalid-regexp": 2, 65 | "no-irregular-whitespace": 2, 66 | "no-iterator": 2, 67 | "no-label-var": 2, 68 | "no-labels": 2, 69 | "no-lone-blocks": 2, 70 | "no-mixed-spaces-and-tabs": 2, 71 | "no-multi-spaces": 2, 72 | "no-multi-str": 2, 73 | "no-multiple-empty-lines": [2, { "max": 1 }], 74 | "no-native-reassign": 0, 75 | "no-negated-in-lhs": 2, 76 | "no-new": 2, 77 | "no-new-func": 2, 78 | "no-new-object": 2, 79 | "no-new-require": 2, 80 | "no-new-wrappers": 2, 81 | "no-obj-calls": 2, 82 | "no-octal": 2, 83 | "no-octal-escape": 2, 84 | "no-proto": 0, 85 | "no-redeclare": 2, 86 | "no-regex-spaces": 2, 87 | "no-return-assign": 2, 88 | "no-self-compare": 2, 89 | "no-sequences": 2, 90 | "no-shadow-restricted-names": 2, 91 | "no-spaced-func": 2, 92 | "no-sparse-arrays": 2, 93 | "no-this-before-super": 2, 94 | "no-throw-literal": 2, 95 | "no-trailing-spaces": 0, 96 | "no-undef": 2, 97 | "no-undef-init": 2, 98 | "no-unexpected-multiline": 2, 99 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 100 | "no-unreachable": 2, 101 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 102 | "no-useless-call": 0, 103 | "no-with": 2, 104 | "one-var": [0, { "initialized": "never" }], 105 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 106 | "padded-blocks": [0, "never"], 107 | "quotes": [2, "single", "avoid-escape"], 108 | "radix": 2, 109 | "semi": [2, "always"], 110 | "semi-spacing": [2, { "before": false, "after": true }], 111 | "space-before-blocks": [2, "always"], 112 | "space-before-function-paren": [2, "never"], 113 | "space-in-parens": [2, "never"], 114 | "space-infix-ops": 2, 115 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 116 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 117 | "use-isnan": 2, 118 | "valid-typeof": 2, 119 | "wrap-iife": [2, "any"], 120 | "yoda": [2, "never"] 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var capture = require('capture-stream'); 5 | var assemble = require('assemble'); 6 | var assert = require('assert'); 7 | var helpers = require('./'); 8 | var app; 9 | 10 | describe('assemble-handlebars-helpers', function() { 11 | beforeEach(function() { 12 | app = assemble(); 13 | app.helpers(helpers); 14 | }); 15 | 16 | it('should register helpers', function() { 17 | assert.deepEqual(Object.keys(app._.helpers.sync), ['partials', 'layouts', 'pages', 'each', 'if', 'log', 'lookup', 'unless', 'with']); 18 | }); 19 | 20 | describe('each', function() { 21 | it('should render using {{each}} helper', function(cb) { 22 | app.page('a.hbs', { content: '{{#each items}}{{this}}{{/each}}' }); 23 | var context = { 24 | items: ['a', 'b', 'c'] 25 | }; 26 | app.render('a.hbs', context, function(err, results) { 27 | if (err) return cb(err); 28 | assert.equal(results.content, 'abc'); 29 | cb(); 30 | }); 31 | }); 32 | 33 | it('should render using {{if}} helper inside {{each}} helper without context loss', function(cb) { 34 | app.page('a.hbs', { content: '{{#each items}}{{#if foo}}{{foo}}{{/if}}{{/each}}' }); 35 | var context = { 36 | items: [{foo: 'a'}, {foo: false}, {foo: 'c'}] 37 | }; 38 | app.render('a.hbs', context, function(err, results) { 39 | if (err) return cb(err); 40 | assert.equal(results.content, 'ac'); 41 | cb(); 42 | }); 43 | }); 44 | }); 45 | 46 | describe('if', function() { 47 | it('should render using {{if}} helper', function(cb) { 48 | app.page('a.hbs', { content: '{{#if foo}}{{foo}}{{/if}}' }); 49 | var context = { 50 | foo: 'bar' 51 | }; 52 | app.render('a.hbs', context, function(err, results) { 53 | if (err) return cb(err); 54 | assert.equal(results.content, 'bar'); 55 | cb(); 56 | }); 57 | }); 58 | }); 59 | 60 | describe('log', function() { 61 | it('should render using {{log}} helper', function(cb) { 62 | app.page('a.hbs', { content: '{{log items}}' }); 63 | var context = { 64 | items: ['a', 'b', 'c'] 65 | }; 66 | 67 | var restore = capture(process.stdout); 68 | app.render('a.hbs', context, function(err, results) { 69 | var output = restore(true); 70 | if (err) return cb(err); 71 | assert.equal(results.content, ''); 72 | assert.equal(output.indexOf("[ 'a', 'b', 'c' ]"), 0); 73 | cb(); 74 | }); 75 | }); 76 | }); 77 | 78 | describe('lookup', function() { 79 | it('should render using {{lookup}} helper', function(cb) { 80 | app.page('a.hbs', { content: '{{#each (lookup data "items")}}{{this}}{{/each}}' }); 81 | var context = { 82 | data: { items: ['a', 'b', 'c'] } 83 | }; 84 | app.render('a.hbs', context, function(err, results) { 85 | if (err) return cb(err); 86 | assert.equal(results.content, 'abc'); 87 | cb(); 88 | }); 89 | }); 90 | 91 | it('should render dynamic partials using {{lookup}} helper', function(cb) { 92 | app.page('b.hbs', { content: '{{> (lookup data "partial") partialData }}'}); 93 | app.partial('partial.hbs', { content: '{{answer}}'}); 94 | var context = { 95 | data: { partial: 'partial' }, 96 | partialData: { answer: '42' } 97 | }; 98 | app.render('b.hbs', context, function(err, results) { 99 | if (err) return cb(err); 100 | assert.equal(results.content, '42'); 101 | cb(); 102 | }); 103 | }); 104 | }); 105 | 106 | describe('unless', function() { 107 | it('should render using {{unless}} helper', function(cb) { 108 | app.page('a.hbs', { content: '{{#unless foo}}{{bar}}{{/unless}}' }); 109 | var context = { 110 | foo: false, 111 | bar: 'baz' 112 | }; 113 | app.render('a.hbs', context, function(err, results) { 114 | if (err) return cb(err); 115 | assert.equal(results.content, 'baz'); 116 | cb(); 117 | }); 118 | }); 119 | }); 120 | 121 | describe('with', function() { 122 | it('should render using {{with}} helper', function(cb) { 123 | app.page('a.hbs', { content: '{{#with data}}{{#each items}}{{this}}{{/each}}{{/with}}' }); 124 | var context = { 125 | data: { items: ['a', 'b', 'c'] } 126 | }; 127 | app.render('a.hbs', context, function(err, results) { 128 | if (err) return cb(err); 129 | assert.equal(results.content, 'abc'); 130 | cb(); 131 | }); 132 | }); 133 | }); 134 | }); 135 | --------------------------------------------------------------------------------