README.mdown in handlebars-0.4.0 vs README.mdown in handlebars-0.5.0
- old
+ new
@@ -8,24 +8,32 @@
### Simple stuff
require 'handlebars'
handlebars = Handlebars::Context.new
- template = handlebars.compile("{{say}}{{what}}")
+ template = handlebars.compile("{{say}} {{what}}")
template.call(:say => "Hey", :what => "Yuh!") #=> "Hey Yuh!"
### functions as properties
- template.call(:say => "Hey", :what => lambda {|this| ("yo" * 2) + "!"}) #=> "Hey yoyo!"
+ template.call(:say => "Hey ", :what => lambda {|this| ("yo" * 2) + "!"}) #=> "Hey yoyo!"
### Block Helpers:
- handlebars.register_helper(:twice) do |block|
- "#{block.call}#{block.call}"
+Just like JavaScript, you can write block helpers with an `{{else}}` section. To print
+out a section twice if a condition is met:
+
+ handlebars.register_helper(:twice) do |context, condition, block|
+ if condition
+ "#{block.fn(context)}#{block.fn(context)}"
+ else
+ block.inverse(context)
+ end
end
- template = handlebars.compile({{#twice}}Hurray!{{/twice}})
- template.call #=> Hurray!Hurray!
+ template = handlebars.compile("{{#twice foo}}Hurray!{{else}}Boo!{{/twice}}")
+ template.call(foo: true) #=> Hurray!Hurray!
+ template.call(foo: false) #=> Boo!
### Safe Strings
By default, handlebars will escape strings that are returned by your block helpers. To
mark a string as safe:
@@ -35,10 +43,10 @@
### Partials
You can directly register partials
- handlebars.register_partial("whoami", " I am {{who}}")
+ handlebars.register_partial("whoami", "I am {{who}}")
handlebars.compile("{{>whoami}}").call(:who => 'Legend') #=> I am Legend
Partials can also be dynamically looked up by defining a partial_missing behavior:
handlebars.partial_missing do |name|