class Tilt::YajlTemplate

Yajl Template implementation

Yajl is a fast JSON parsing and encoding library for Ruby See github.com/brianmario/yajl-ruby

The template source is evaluated as a Ruby string, and the result is converted to_json.

Example

# This is a template example.
# The template can contain any Ruby statement.
tpl <<-EOS
  @counter = 0

  # The json variable represents the buffer
  # and holds the data to be serialized into json.
  # It defaults to an empty hash, but you can override it at any time.
  json = {
    :"user#{@counter += 1}" => { :name => "Joshua Peek", :id => @counter },
    :"user#{@counter += 1}" => { :name => "Ryan Tomayko", :id => @counter },
    :"user#{@counter += 1}" => { :name => "Simone Carletti", :id => @counter },
  }

  # Since the json variable is a Hash,
  # you can use conditional statements or any other Ruby statement
  # to populate it.
  json[:"user#{@counter += 1}"] = { :name => "Unknown" } if 1 == 2

  # The last line doesn't affect the returned value.
  nil
EOS

template = Tilt::YajlTemplate.new { tpl }
template.render(self)