Skip to content
Browse files

add section support

  • Loading branch information...
1 parent 7f98562 commit e888d521ba0a724ff7e50d73b7ee48592b90db94 @tj tj committed
Showing with 95 additions and 9 deletions.
  1. +8 −6 Readme.md
  2. +31 −3 index.js
  3. +56 −0 test/index.js
View
14 Readme.md
@@ -51,15 +51,17 @@ module.exports = function(obj) {
Compile the mustache `string` to a stand-alone `Function` accepting a context `obj`.
-## Todo
+## Divergence
- I just basically needed interpolation for now, but I'll add this stuff later:
+ Minstache also allows `{` as an alias of `{{` to reduce the noise.
- - sections
- - inverted sections
- - lambdas
+ 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}` syntax behaves as a conditional 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.
- No partials, 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.
+## Todo
+
+ - lambdas
## License
View
34 index.js
@@ -44,8 +44,25 @@ function compile(str) {
if (i % 2 == 0) {
js.push('"' + tok.replace(/"/g, '\\"') + '"');
} else {
- if (!tok.match(/^[\w.]+$/)) throw new Error('invalid property "' + tok + '"');
- js.push(' + escape(obj.' + tok + ') + ');
+ switch (tok[0]) {
+ case '/':
+ tok = tok.slice(1);
+ js.push(' : "") + ');
+ break;
+ case '^':
+ tok = tok.slice(1);
+ assertProperty(tok);
+ js.push(' + (!obj.' + tok + ' ? ');
+ break;
+ case '#':
+ tok = tok.slice(1);
+ assertProperty(tok);
+ js.push(' + (obj.' + tok + ' ? ');
+ break;
+ default:
+ assertProperty(tok);
+ js.push(' + escape(obj.' + tok + ') + ');
+ }
}
}
@@ -56,6 +73,17 @@ function compile(str) {
}
/**
+ * Assert that `prop` is a valid property.
+ *
+ * @param {String} prop
+ * @api private
+ */
+
+function assertProperty(prop) {
+ if (!prop.match(/^[\w.]+$/)) throw new Error('invalid property "' + prop + '"');
+}
+
+/**
* Parse `str`.
*
* @param {String} str
@@ -64,7 +92,7 @@ function compile(str) {
*/
function parse(str) {
- return str.split(/\{\{|\}\}/);
+ return str.split(/\{\{?|\}?\}/);
}
/**
View
56 test/index.js
@@ -5,6 +5,13 @@
var mm = require('..');
+describe('{id}', function(){
+ it('should work too', function(){
+ var user = { name: 'tobi' };
+ mm('hi {name}.', user).should.equal('hi tobi.');
+ })
+})
+
describe('{{id}}', function(){
it('should buffer', function(){
var user = { name: 'tobi' };
@@ -39,3 +46,52 @@ describe('{{id}}', function(){
}
})
})
+
+describe('{{#id}}', function(){
+ it('should pass through when truthy', function(){
+ var user = { admin: true };
+ mm('{{#admin}}yup{{/admin}}', user).should.equal('yup');
+ })
+
+ it('should ignore when falsey', function(){
+ var user = { admin: false };
+ mm('admin: {{#admin}}yup{{/admin}}', user).should.equal('admin: ');
+ })
+
+ it('should ignore when undefined', function(){
+ var user = {};
+ mm('admin: {{#admin}}yup{{/admin}}', user).should.equal('admin: ');
+ })
+
+ 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');
+ })
+
+ it('should support nested conditionals', function(){
+ var user = { admin: true, authenticated: true };
+ mm('{{#admin}}{{#authenticated}}yup{{/}}{{/}}', user).should.equal('yup');
+ })
+})
+
+describe('{{^id}}', function(){
+ it('should ignore when truthy', function(){
+ var user = { admin: true };
+ mm('{{^admin}}yup{{/admin}}', user).should.equal('');
+ })
+
+ it('should pass through when falsey', function(){
+ var user = { admin: false };
+ mm('admin: {{^admin}}nope{{/admin}}', user).should.equal('admin: 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');
+ })
+
+ it('should support nested conditionals', function(){
+ var user = { admin: false, authenticated: false };
+ mm('{{^admin}}{{^authenticated}}nope{{/}}{{/}}', user).should.equal('nope');
+ })
+})

0 comments on commit e888d52

Please sign in to comment.
Something went wrong with that request. Please try again.