Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support nested props in conditionals #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ function compile(str) {
case '^':
tok = tok.slice(1);
assertProperty(tok);
js.push(' + section(obj, "' + tok + '", true, ');
js.push(' + section(obj, obj.' + tok + ', true, ');
break;
case '#':
tok = tok.slice(1);
assertProperty(tok);
js.push(' + section(obj, "' + tok + '", false, ');
js.push(' + section(obj, obj.' + tok + ', false, ');
break;
case '!':
tok = tok.slice(1);
Expand Down Expand Up @@ -117,15 +117,14 @@ function indent(str) {
/**
* Section handler.
*
* @param {Object} context obj
* @param {String} prop
* @param {String} str
* @param {Object} obj
* @param {Mixed} val
* @param {Boolean} negate
* @param {String} str
* @api private
*/

function section(obj, prop, negate, str) {
var val = obj[prop];
function section(obj, val, negate, str) {
if ('function' == typeof val) return val.call(obj, str);
if (negate) val = !val;
if (val) return str;
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ describe('{{#id}}', function(){
mm('admin: {{#admin}}yup{{/admin}}', user).should.equal('admin: ');
})

it('should support nested props', function(){
var obj = { user: { admin: true }};
mm('{{#user.admin}}yup{{/user.admin}}', obj).should.equal('yup');
})

it('should support nested tags', function(){
var user = { admin: true, name: 'tobi' };
mm('{{#admin}}{{name}} is an admin{{/admin}}', user).should.equal('tobi is an admin');
Expand Down Expand Up @@ -103,6 +108,11 @@ describe('{{^id}}', function(){
mm('admin: {{^admin}}nope{{/admin}}', user).should.equal('admin: nope');
})

it('should support nested props', function(){
var obj = { user: { admin: false }};
mm('{{^user.admin}}nope{{/user.admin}}', obj).should.equal('nope');
})

it('should support nested tags', function(){
var user = { admin: false, name: 'tobi' };
mm('{{^admin}}{{name}} is not an admin{{/admin}}', user).should.equal('tobi is not an admin');
Expand Down