Permalink
Browse files

Added first-class `each` / `for` support

  • Loading branch information...
1 parent dd1acf0 commit e16bc02e6a38b8a9c33f0bf77abf140dc1fee578 @tj tj committed Aug 25, 2011
Showing with 43 additions and 3 deletions.
  1. +13 −1 Readme.md
  2. +2 −2 lib/lexer.js
  3. +28 −0 test/jade.test.js
View
@@ -20,7 +20,7 @@
- combine dynamic and static tag classes
- parse tree manipulation via _filters_
- supports [Express JS](http://expressjs.com) out of the box
- - transparent iteration over objects, arrays, and even non-enumerables via `- each`
+ - transparent iteration over objects, arrays, and even non-enumerables via `each`
- block comments
- no tag prefix
- AST filters
@@ -601,6 +601,18 @@ versions of Jade will simply render:
<textarea></textarea>
+ Jade also now supports `each` without the code prefix (`-`):
+
+ each user in users
+ each role in user.roles
+ li= role
+
+ You may also use `for` if you prefer:
+
+ for user in users
+ for role in user.roles
+ li= role
+
## Includes
Includes allow you to statically include chunks of Jade
View
@@ -268,7 +268,7 @@ Lexer.prototype = {
each: function() {
var captures;
- if (captures = /^- *each *(\w+)(?: *, *(\w+))? * in *([^\n]+)/.exec(this.input)) {
+ if (captures = /^(?:- *)?(?:each|for) *(\w+)(?: *, *(\w+))? * in *([^\n]+)/.exec(this.input)) {
this.consume(captures[0].length);
var tok = this.tok('each', captures[1]);
tok.key = captures[2] || 'index';
@@ -543,9 +543,9 @@ Lexer.prototype = {
|| this.doctype()
|| this.include()
|| this.mixin()
+ || this.each()
|| this.tag()
|| this.filter()
- || this.each()
|| this.code()
|| this.id()
|| this.className()
View
@@ -800,6 +800,34 @@ module.exports = {
].join('');
assert.equal(html, render(str));
+
+ var str = [
+ '- var users = ["tobi", "loki", "jane"]',
+ 'each user in users',
+ ' li= user',
+ ].join('\n');
+
+ var html = [
+ '<li>tobi</li>',
+ '<li>loki</li>',
+ '<li>jane</li>',
+ ].join('');
+
+ assert.equal(html, render(str));
+
+ var str = [
+ '- var users = ["tobi", "loki", "jane"]',
+ 'for user in users',
+ ' li= user',
+ ].join('\n');
+
+ var html = [
+ '<li>tobi</li>',
+ '<li>loki</li>',
+ '<li>jane</li>',
+ ].join('');
+
+ assert.equal(html, render(str));
},
'test renderFile() fs exception': function(assert, beforeExit){

0 comments on commit e16bc02

Please sign in to comment.