Permalink
Browse files

add lambda support

  • Loading branch information...
1 parent bfb5073 commit 8746f2a1cca49d26ecdcd6c3c2cdcd3b769d8c10 @tj tj committed Sep 5, 2012
Showing with 50 additions and 11 deletions.
  1. +0 −6 Readme.md
  2. +38 −4 index.js
  3. +12 −1 test/index.js
View
@@ -63,12 +63,6 @@ module.exports = function anonymous(obj) {
Partials are not supported, this lib is meant to be a small template engine solution for stand-alone [component](http://github.com/component) templates. If your template takes "partials" then pass other rendered strings to it. If you need a full-blown mustache solution Hogan.js is still great.
- The `{#foo}` / `{^foo}` syntax behave as conditionals only, iteration implies you're doing too much. If you have a list of users, have a "user" view and a "user-list" view. Maybe we'll add this in the future but for now `{#foo}{/foo}` compiles to a ternary.
-
-## Todo
-
- - lambdas
-
## License
(The MIT License)
View
@@ -47,17 +47,17 @@ function compile(str) {
switch (tok[0]) {
case '/':
tok = tok.slice(1);
- js.push(' : "") + ');
+ js.push(') + ');
break;
case '^':
tok = tok.slice(1);
assertProperty(tok);
- js.push(' + (!obj.' + tok + ' ? ');
+ js.push(' + section(obj, "' + tok + '", true, ');
break;
case '#':
tok = tok.slice(1);
assertProperty(tok);
- js.push(' + (obj.' + tok + ' ? ');
+ js.push(' + section(obj, "' + tok + '", false, ');
break;
default:
assertProperty(tok);
@@ -67,7 +67,11 @@ function compile(str) {
}
js = js.join('').replace(/\n/g, '\\n');
- js = escape.toString().replace(/^/gm, ' ') + ';\n return ' + js;
+
+ js = '\n'
+ + indent(escape.toString()) + ';\n\n'
+ + indent(section.toString()) + ';\n\n'
+ + ' return ' + js;
return new Function('obj', js);
}
@@ -96,6 +100,36 @@ function parse(str) {
}
/**
+ * Indent `str`.
+ *
+ * @param {String} str
+ * @return {String}
+ * @api private
+ */
+
+function indent(str) {
+ return str.replace(/^/gm, ' ');
+}
+
+/**
+ * Section handler.
+ *
+ * @param {Object} context obj
+ * @param {String} prop
+ * @param {String} str
+ * @param {Boolean} negate
+ * @api private
+ */
+
+function section(obj, prop, negate, str) {
+ var val = obj[prop];
+ if ('function' == typeof val) return val.call(obj, str);
+ if (negate) val = !val;
+ if (val) return str;
+ return '';
+}
+
+/**
* Escape the given `html`.
*
* @param {String} html
View
@@ -72,6 +72,17 @@ describe('{{#id}}', function(){
var user = { admin: true, authenticated: true };
mm('{{#admin}}{{#authenticated}}yup{{/}}{{/}}', user).should.equal('yup');
})
+
+ it('should support lambdas', function(){
+ var obj = {
+ md: function(str){
+ return str.replace(/_(.*?)_/g, '<em>$1</em>');
+ }
+ };
+
+ mm('{#md}some _markdown_ awesome!{/md}', obj)
+ .should.equal('some <em>markdown</em> awesome!');
+ })
})
describe('{{^id}}', function(){
@@ -94,4 +105,4 @@ describe('{{^id}}', function(){
var user = { admin: false, authenticated: false };
mm('{{^admin}}{{^authenticated}}nope{{/}}{{/}}', user).should.equal('nope');
})
-})
+})

0 comments on commit 8746f2a

Please sign in to comment.