Skip to content

Instantly share code, notes, and snippets.

@jimsynz
Created September 18, 2012 22:01
Show Gist options
  • Save jimsynz/3746206 to your computer and use it in GitHub Desktop.
Save jimsynz/3746206 to your computer and use it in GitHub Desktop.
This may seem like a contrived example but it comes from the moustache compatibility tests in Handlebars.js' acceptance suite.
require 'flavour_saver'
# User's helper code:
FS.register_helper(:link) do |prefix,&block|
"<a href='#{prefix}/#{url}'>#{text}</a>"
end
Link = Struct.new(:url, :text)
Context = Struct.new(:prefix, :links)
FS.evaluate(File.read("template.handlebars"), Context.new('foo', [Link.new("abc", "A B C"), Link.new("def", "D E F"))
# Should return:
# <a href='foo/abc'>A B C</a>
# <a href='foo/def'>D E F</a>
# But doesn't because #url and #text NameError inside the block
# is not evaluated inside the correct context. Complication is
# that the helper block can optionally take a block (ie it's a
# handlebars block helper.
{{#each links}}
{{link ../prefix}}
{{/each}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment