Permalink
Browse files

Added first-class `if`, `unless`, `else` and `else if` support

  • Loading branch information...
1 parent 617acb7 commit d1ce9d9052f54bdbddc8b3e5b1a340c4ab1323ce @tj tj committed Aug 25, 2011
Showing with 106 additions and 0 deletions.
  1. +28 −0 Readme.md
  2. +23 −0 lib/lexer.js
  3. +55 −0 test/jade.test.js
View
28 Readme.md
@@ -604,6 +604,34 @@ JavaScript:
for role in user.roles
li= role
+## Conditionals
+
+ Jade conditionals are equivalent to those using the code (`-`) prefix,
+ however allow you to ditch parenthesis to become more designer friendly,
+ however keep in mind the expression given is _regular_ JavaScript:
+
+ for user in users
+ if user.role == 'admin'
+ p #{user.name} is an admin
+ else
+ p= user.name
+
+ is equivalent to the following using vanilla JavaScript literals:
+
+ for user in users
+ - if (user.role == 'admin')
+ p #{user.name} is an admin
+ - else
+ p= user.name
+
+ Jade also provides have `unless` which is equivalent to `if (!(expr))`:
+
+ for user in users
+ unless user.isAnonymous
+ p
+ | Click to view
+ a(href='/users/' + user.id)= user.name
+
## Includes
Includes allow you to statically include chunks of Jade
View
23 lib/lexer.js
@@ -263,6 +263,28 @@ Lexer.prototype = {
},
/**
+ * Conditional.
+ */
+
+ conditional: function() {
+ var captures;
+ if (captures = /^(if|unless|else if|else) *([^\n]*)/.exec(this.input)) {
+ this.consume(captures[0].length);
+ var type = captures[1]
+ , js = captures[2];
+
+ switch (type) {
+ case 'if': js = 'if (' + js + ')'; break;
+ case 'unless': js = 'if (!(' + js + '))'; break;
+ case 'else if': js = 'else if (' + js + ')'; break;
+ case 'else': js = 'else'; break;
+ }
+
+ return this.tok('code', js);
+ }
+ },
+
+ /**
* Each.
*/
@@ -543,6 +565,7 @@ Lexer.prototype = {
|| this.doctype()
|| this.include()
|| this.mixin()
+ || this.conditional()
|| this.each()
|| this.tag()
|| this.filter()
View
55 test/jade.test.js
@@ -830,6 +830,61 @@ module.exports = {
assert.equal(html, render(str));
},
+ 'test if': function(assert){
+ var str = [
+ '- var users = ["tobi", "loki", "jane"]',
+ 'if users.length',
+ ' p users: #{users.length}',
+ ].join('\n');
+
+ assert.equal('<p>users: 3</p>', render(str));
+ },
+
+ 'test unless': function(assert){
+ var str = [
+ '- var users = ["tobi", "loki", "jane"]',
+ 'unless users.length',
+ ' p no users',
+ ].join('\n');
+
+ assert.equal('', render(str));
+
+ var str = [
+ '- var users = []',
+ 'unless users.length',
+ ' p no users',
+ ].join('\n');
+
+ assert.equal('<p>no users</p>', render(str));
+ },
+
+ 'test else': function(assert){
+ var str = [
+ '- var users = []',
+ 'if users.length',
+ ' p users: #{users.length}',
+ 'else',
+ ' p users: none',
+ ].join('\n');
+
+ assert.equal('<p>users: none</p>', render(str));
+ },
+
+ 'test else if': function(assert){
+ var str = [
+ '- var users = ["tobi", "jane", "loki"]',
+ 'for user in users',
+ ' if user == "tobi"',
+ ' p awesome #{user}',
+ ' else if user == "jane"',
+ ' p lame #{user}',
+ ' else',
+ ' p #{user}',
+ ].join('\n');
+
+ assert.equal('<p>awesome tobi</p><p>lame jane</p><p>loki</p>', render(str));
+ },
+
'test renderFile() fs exception': function(assert, beforeExit){
var called;
jade.renderFile('foo', function(err, str){

0 comments on commit d1ce9d9

Please sign in to comment.