Skip to content

Instantly share code, notes, and snippets.

@defunkt
Created May 18, 2010 01:19
Show Gist options
  • Save defunkt/404480 to your computer and use it in GitHub Desktop.
Save defunkt/404480 to your computer and use it in GitHub Desktop.
Section separators in mustache.rb
require 'mustache'
require 'yaml'
class Sep < Mustache
def last?
self[:to_s] == self[:names].last.to_s
end
end
template = <<template
Gem Names:
* {{#names}}{{to_s}}{{^last?}}{{separator}}{{/last?}}{{/names}}{{! hack}}
* {{#names}}{{to_s}}{{^last?}}, {{/last?}}{{/names}}{{! hack}}
template
data = YAML.load(<<yaml)
names: [ mustache, sinatra, rack]
separator: " - "
yaml
puts Sep.render(template, data)
$ ruby sep.rb
Gem Names:
* mustache - sinatra - rack
* mustache, sinatra, rack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment