Permalink
Browse files

added existence sections

  • Loading branch information...
1 parent 0837287 commit a9d4d09fa43c0e5305b059b26076364576b7424b @amccollum committed Oct 4, 2011
Showing with 50 additions and 12 deletions.
  1. +13 −1 README.md
  2. +8 −1 lib/wings.js
  3. +1 −1 package.json
  4. +20 −8 src/test/wings-test.coffee
  5. +8 −1 src/wings/wings.coffee
View
14 README.md
@@ -95,8 +95,20 @@ replace the section.
#### Inverted Sections ####
Inverted sections are like normal sections, except that the section will
-only be rendered if the value is false or the given array is empty.
+only be rendered if the value is false or the given array is empty:
+ {!foo}
+ The foo value was false.
+ {/foo}
+
+
+#### Existence Sections ####
+
+Existence sections are rendered when the value is present.
+
+ {?foo}
+ The foo value was defined, but may be false.
+ {/foo}
Acknowledgements
----------------
View
9 lib/wings.js
@@ -31,14 +31,21 @@
}
});
};
- parse_re = /\s*\{([!:])\s*([^}]*?)\s*\}([\S\s]+?)\s*\{\/\s*\2\s*\}|\{(\#)\s*[\S\s]+?\s*\#\}|\{([@&]?)\s*([^}]*?)\s*\}/mg;
+ parse_re = /\s*\{([:!?])\s*([^}]*?)\s*\}([\S\s]+?)\s*\{\/\s*\2\s*\}|\{(\#)\s*[\S\s]+?\s*\#\}|\{([@&]?)\s*([^}]*?)\s*\}/mg;
return renderRawTemplate = function(template, data, links) {
return template.replace(parse_re, function(match, section_op, section_name, section_content, comment_op, tag_op, tag_name) {
var content, i, link, name, op, part, parts, v, value, _len, _ref2;
op = section_op || comment_op || tag_op;
name = section_name || tag_name;
content = section_content;
switch (op) {
+ case '?':
+ if (name in data) {
+ return renderRawTemplate(content, data, links);
+ } else {
+ return "";
+ }
+ break;
case ':':
value = data[name];
if (!(value != null)) {
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "wings",
- "version": "0.4.0",
+ "version": "0.5.0",
"description": "Templating library that works on the server and client closely modeled on Mustache",
"keywords": ["ender", "template", "mustache", "html"],
"author": "Andrew McCollum <amccollum@gmail.com>",
View
28 src/test/wings-test.coffee
@@ -93,18 +93,30 @@ vows.add 'templates'
'sections:':
+ 'templates with existence sections':
+ topic: [
+ t('{?absent}foo{/absent}bar', {}),
+ t('{?exists}foo{/exists}bar', {exists: true}),
+ t('{?falsy}foo{/falsy}bar', {falsy: false}),
+ ]
+
+ 'should only include the section when the tag is defined': (topics) ->
+ equal topics[0], 'bar'
+ equal topics[1], 'foobar'
+ equal topics[2], 'foobar'
+
'templates with regular sections':
topic: [
- t('{:untruthy}foo{/untruthy}bar', {untruthy: 0}),
- t('{:untruthy}foo{/untruthy}bar', {untruthy: []}),
- t('{:untruthy}foo{/untruthy}bar', {untruthy: false}),
+ t('{:falsy}foo{/falsy}bar', {falsy: 0}),
+ t('{:falsy}foo{/falsy}bar', {falsy: []}),
+ t('{:falsy}foo{/falsy}bar', {falsy: false}),
t('{:truthy}foo{/truthy}bar', {truthy: 1}),
t('{:truthy}foo{/truthy}bar', {truthy: {}}),
t('{:truthy}foo{/truthy}bar', {truthy: true}),
]
- 'should only include those sections when the tag is truthy': (topics) ->
+ 'should only include the section when the tag is truthy': (topics) ->
equal topics[0], 'bar'
equal topics[1], 'bar'
equal topics[2], 'bar'
@@ -114,16 +126,16 @@ vows.add 'templates'
'templates with inverse sections':
topic: [
- t('{!untruthy}foo{/untruthy}bar', {untruthy: 0}),
- t('{!untruthy}foo{/untruthy}bar', {untruthy: []}),
- t('{!untruthy}foo{/untruthy}bar', {untruthy: false}),
+ t('{!falsy}foo{/falsy}bar', {falsy: 0}),
+ t('{!falsy}foo{/falsy}bar', {falsy: []}),
+ t('{!falsy}foo{/falsy}bar', {falsy: false}),
t('{!truthy}foo{/truthy}bar', {truthy: 1}),
t('{!truthy}foo{/truthy}bar', {truthy: {}}),
t('{!truthy}foo{/truthy}bar', {truthy: true}),
]
- 'should only include those sections when the tag is not truthy': (topics) ->
+ 'should only include the section when the tag is not truthy': (topics) ->
equal topics[0], 'foobar'
equal topics[1], 'foobar'
equal topics[2], 'foobar'
View
9 src/wings/wings.coffee
@@ -22,7 +22,7 @@
else return s
parse_re = ///
- \s* \{([!:]) \s* ([^}]*?) \s* \} ([\S\s]+?) \s* \{/ \s* \2 \s*\} | # sections
+ \s* \{([:!?]) \s* ([^}]*?) \s* \} ([\S\s]+?) \s* \{/ \s* \2 \s*\} | # sections
\{(\#) \s* [\S\s]+? \s* \#\} | # comments
\{([@&]?) \s* ([^}]*?) \s* \} # tags
///mg
@@ -34,6 +34,13 @@
content = section_content
switch op
+ when '?' # existence section
+ if name of data
+ return renderRawTemplate(content, data, links)
+
+ else
+ return ""
+
when ':' # section
value = data[name]
if not value?

0 comments on commit a9d4d09

Please sign in to comment.