From 7589aa70439fd58b6f3da60267bb5d87bd0089dc Mon Sep 17 00:00:00 2001 From: George Stagas Date: Thu, 3 Oct 2013 14:54:52 +0300 Subject: [PATCH] support nested props in conditionals --- index.js | 13 ++++++------- test/index.js | 10 ++++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 0ba8f94..6b65991 100644 --- a/index.js +++ b/index.js @@ -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); @@ -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; diff --git a/test/index.js b/test/index.js index 003a8b2..c0226d5 100644 --- a/test/index.js +++ b/test/index.js @@ -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'); @@ -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');