class Tilt::YajlTemplate
template.render(self)
template = Tilt::YajlTemplate.new { tpl }
EOS
nil
# The last line doesn’t affect the returned value.
json[:“user#{@counter += 1}”] = { :name => “Unknown” } if 1 == 2
# to populate it.
# you can use conditional statements or any other Ruby statement
# Since the json variable is a Hash,
}
:“user#{@counter += 1}” => { :name => “Simone Carletti”, :id => @counter },
:“user#{@counter += 1}” => { :name => “Ryan Tomayko”, :id => @counter },
:“user#{@counter += 1}” => { :name => “Joshua Peek”, :id => @counter },
json = {
# It defaults to an empty hash, but you can override it at any time.
# and holds the data to be serialized into json.
# The json variable represents the buffer
@counter = 0
tpl <<-EOS
# The template can contain any Ruby statement.
# This is a template example.
== Example
and the result is converted #to_json.
The template source is evaluated as a Ruby string,
See github.com/brianmario/yajl-ruby<br>Yajl is a fast JSON parsing and encoding library for Ruby
Yajl Template implementation
def self.engine_initialized?
def self.engine_initialized? defined? ::Yajl end
def decorate(json)
options - The option Hash to customize the behavior.
json - The json String to decorate.
Decorates the +json+ input according to given +options+.
def decorate(json) callback, variable = options[:callback], options[:variable] if callback && variable "var #{variable} = #{json}; #{callback}(#{variable});" elsif variable "var #{variable} = #{json};" elsif callback "#{callback}(#{json});" else json end end
def evaluate(scope, locals, &block)
def evaluate(scope, locals, &block) decorate super(scope, locals, &block) end
def initialize_engine
def initialize_engine require_template_library 'yajl' end
def precompiled_postamble(locals)
def precompiled_postamble(locals) "Yajl::Encoder.new.encode(json)" end
def precompiled_preamble(locals)
def precompiled_preamble(locals) return super if locals.include? :json "json = {}\n#{super}" end
def precompiled_template(locals)
def precompiled_template(locals) data.to_str end
def prepare
def prepare end