Skip to content

Instantly share code, notes, and snippets.

@defunkt
Created May 24, 2010 23:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save defunkt/412565 to your computer and use it in GitHub Desktop.
Save defunkt/412565 to your computer and use it in GitHub Desktop.
{{.}} in mustache.rb
require 'mustache'
Mustache::Parser::ALLOWED_CONTENT = /(\w|[?!\/.-])*/
class ImplicitIterator < Mustache
define_method "." do
self[:to_s]
end
def names
%w( one two three )
end
end
puts ImplicitIterator.render(<<template, {})
{{#names}}
{{.}}
{{/names}}
template
@judofyr
Copy link

judofyr commented May 26, 2010

Nice hack, even though I would prefer a compiler which converts [:mustache, :{e,u}tag, "."] to [:mustache, :{e,u}tag, "to_s"]. Oh well, Mustache doesn't support adding your own compilers to the chain, does it?

@defunkt
Copy link
Author

defunkt commented May 26, 2010

Agreed - "macros" or a similar concept would rock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment