Created
September 18, 2012 22:01
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment