Permalink
Browse files

add JSON tags

  • Loading branch information...
1 parent 9609311 commit 7082246afe230917d7312fae9fbaca3675d8e898 @amccollum committed Aug 19, 2013
Showing with 32 additions and 15 deletions.
  1. +1 −1 Cakefile
  2. +1 −2 lib/ender.js
  3. +15 −8 lib/wings.js
  4. +7 −1 src/test/wings-test.coffee
  5. +8 −3 src/wings/wings.coffee
View
@@ -28,7 +28,7 @@ task 'test', 'Build and run the test suite', ->
'ln -sf ../node_modules node_modules',
'ln -sf .. node_modules/wings',
#'node_modules/.bin/ender build ender-vows ..',
- 'node_modules/.bin/vows *-test.js',
+ 'node_modules/.bin/vows --spec *-test.js',
'rm -f node_modules/wings node_modules/vows node_modules',
'cd ..',
]
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -7,7 +7,7 @@ equal = assert.equal
if not vows.add
vows.add = (name, batch) -> vows.describe(name).addBatch(batch).export(module)
-vows.add 'templates'
+vows.add 'templates',
'basics:':
'an empty template':
topic: t('')
@@ -71,6 +71,12 @@ vows.add 'templates'
'should escape the html reserved characters': (topic) ->
equal topic, 'This shouldn\'t produce html: <b>bolded</b>'
+ 'a template with JSON tags':
+ topic: t('This should produce json: {~ob}', { ob: ['a', 1, { 'b': 3 }]})
+
+ 'should produce json': (topic) ->
+ equal topic, 'This should produce json: ["a",1,{"b":3}]'
+
'a template with unescaped tags':
topic: t('This should produce html: {&html}', {html: '<b>bolded</b>'})
View
@@ -26,7 +26,7 @@
parsePattern = ///
\{([:!]) \s* ([^}\s]*?) \s* \} ([\S\s]+?) \{/ \s* \2 \s* \} | # sections
\{(\#) [\S\s]+? \#\} | # comments
- \{([@&]?) \s* ([^}\s]*?) \s* \} # tags
+ \{([@&~]?) \s* ([^}\s]*?) \s* \} # tags
///mg
renderRawTemplate = (template, data, links) ->
@@ -95,7 +95,7 @@
return renderRawTemplate(replaceBraces(link), data, links)
- when '&', '' # value tag
+ when '~', '&', '' # value tag
value = data
rest = name
while value and rest
@@ -111,7 +111,12 @@
else if typeof value == 'function'
value = value.call(data)
- return (if op == '&' then value else escapeXML(value))
+ if op == '~'
+ return JSON.stringify(value)
+ else if op == '&'
+ return value
+ else
+ return escapeXML(value)
else
throw "Invalid section op: #{op}"

0 comments on commit 7082246

Please sign in to comment.