Skip to content
Browse files

Added `yield`. Closes #16.

  • Loading branch information...
1 parent 177bcc3 commit e5157ec445318b6ac62be080d596541a60250807 @mauricemach committed Sep 14, 2011
Showing with 34 additions and 1 deletion.
  1. +1 −0 CHANGELOG.md
  2. +8 −0 docs/coffeekup.html
  3. +11 −0 docs/reference.md
  4. +2 −1 package.json
  5. +8 −0 src/coffeekup.coffee
  6. +4 −0 test.coffee
View
1 CHANGELOG.md
@@ -2,6 +2,7 @@
- Fixed #71: indexOf not supported in IE7 (thanks @jaekwon).
- Added better error reporting to express adapter. [benekastah]
+ - Added `yield`: see `/docs/reference.md`. Closes #16 (thanks @pyrotechnick, @colinta and @smathy).
**v0.3.0** (2011-09-04):
View
8 docs/coffeekup.html
@@ -160,6 +160,14 @@
<span class="nx">__ck</span><span class="p">.</span><span class="nx">render_tag</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">idclass</span><span class="p">,</span> <span class="nx">attrs</span><span class="p">,</span> <span class="nx">contents</span><span class="p">)</span>
+ <span class="nv">yield = </span><span class="nf">(f) -&gt;</span>
+ <span class="nv">temp_buffer = </span><span class="p">[]</span>
+ <span class="nv">old_buffer = </span><span class="nx">__ck</span><span class="p">.</span><span class="nx">buffer</span>
+ <span class="nv">__ck.buffer = </span><span class="nx">temp_buffer</span>
+ <span class="nx">f</span><span class="p">()</span>
+ <span class="nv">__ck.buffer = </span><span class="nx">old_buffer</span>
+ <span class="nx">temp_buffer</span><span class="p">.</span><span class="nx">join</span> <span class="s1">&#39;&#39;</span>
+
<span class="nv">h = </span><span class="nf">(txt) -&gt;</span>
<span class="nb">String</span><span class="p">(</span><span class="nx">txt</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/&amp;/g</span><span class="p">,</span> <span class="s1">&#39;&amp;amp;&#39;</span><span class="p">)</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/&lt;/g</span><span class="p">,</span> <span class="s1">&#39;&amp;lt;&#39;</span><span class="p">)</span>
View
11 docs/reference.md
@@ -185,6 +185,17 @@ CoffeeScript-friendly shortcut to `script`:
coffeescript src: 'script.coffee'
<script type="text/coffeescript" src="script.coffee"></script>
+#### yield
+
+Returns the output of a template chunk as a string instead of writing it to the buffer. Useful for string interpolations. Ex.:
+
+ p "This text could use #{yield -> a href: '/', 'a link'}."
+ <p>This text could use <a href="/">a link</a>.</p>
+
+Without it, the `a` function runs first, writes to the buffer and returns `null`, resulting in a useless output:
+
+ p "This text could use #{a href: '/', 'a link'}."
+ <a href="/">a link</a><p>This text could use null.</p>
#### @
View
3 package.json
@@ -18,8 +18,9 @@
"James Campos <james.r.campos@gmail.com>",
"Martin Westergaard Lassen <martin@mwl.dk>",
"Paul Harper <benekastah@gmail.com>",
+ "Colin Thomas-Arnold <colinta@mac.com>",
"Esa-Matti Suuronen <esa-matti@suuronen.org>",
- "Jason King <jasonk@amcoonline.net>",
+ "Jason King <jk@handle.it>",
"Brandon Bloom <brandon@brandonbloom.name>",
"Nicholas Kinsey <nicholas.kinsey@feistystudios.com>"
]
View
8 src/coffeekup.coffee
@@ -205,6 +205,14 @@ skeleton = (data = {}) ->
__ck.render_tag(name, idclass, attrs, contents)
+ yield = (f) ->
@ewollesen
ewollesen added a note Oct 19, 2011

Yield was introduced as a reserved word in JavaScript 1.7. It is currently only supported by Firefox, but is slated for future versions of ECMAScript, which should be supported by all major browsers. As a result, it might be wise to alter the implementation to avoid the use of the term yield.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ temp_buffer = []
+ old_buffer = __ck.buffer
+ __ck.buffer = temp_buffer
+ f()
+ __ck.buffer = old_buffer
+ temp_buffer.join ''
+
h = (txt) ->
String(txt).replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
View
4 test.coffee
@@ -160,6 +160,10 @@ tests =
'''
params: {format: yes}
+
+ 'yield':
+ template: "p \"This text could use \#{yield -> strong -> a href: '/', 'a link'}.\""
+ expected: '<p>This text could use <strong><a href="/">a link</a></strong>.</p>'
ck = require './src/coffeekup'
render = ck.render

0 comments on commit e5157ec

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