Skip to content

Commit

Permalink
Added yield. Closes #16.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricemach committed Sep 14, 2011
1 parent 177bcc3 commit e5157ec
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
8 changes: 8 additions & 0 deletions docs/coffeekup.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
11 changes: 11 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>

#### @

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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>"
]
Expand Down
8 changes: 8 additions & 0 deletions src/coffeekup.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ skeleton = (data = {}) ->

__ck.render_tag(name, idclass, attrs, contents)

yield = (f) ->

This comment has been minimized.

Copy link
@ewollesen

ewollesen 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.

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;')
Expand Down
4 changes: 4 additions & 0 deletions test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e5157ec

Please sign in to comment.