Class: Tilt::YajlTemplate

### Home Classes Methods

### In Files

- lib/tilt/yajl.rb

### Parent

Tilt::Template

### Methods

- ::engine_initialized?
- #decorate
- #evaluate
- #initialize_engine
- #precompiled_postamble
- #precompiled_preamble
- #precompiled_template
- #prepare

### Class/Module Index

Quicksearch

- Tilt
- Tilt::AsciidoctorTemplate
- Tilt::BlueClothTemplate
- Tilt::BuilderTemplate
- Tilt::CSVTemplate
- Tilt::Cache
- Tilt::CoffeeScriptTemplate
- Tilt::CompileSite
- Tilt::CreoleTemplate
- Tilt::ERBTemplate
- Tilt::ErubisTemplate
- Tilt::EtanniTemplate
- Tilt::HamlTemplate
- Tilt::KramdownTemplate
- Tilt::LessTemplate
- Tilt::LiquidTemplate
- Tilt::MarkabyTemplate
- Tilt::MarukuTemplate
- Tilt::NokogiriTemplate
- Tilt::PlainTemplate
- Tilt::RDiscountTemplate
- Tilt::RDocTemplate
- Tilt::RadiusTemplate
- Tilt::RedClothTemplate
- Tilt::RedcarpetTemplate
- Tilt::RedcarpetTemplate::Redcarpet1
- Tilt::RedcarpetTemplate::Redcarpet2
- Tilt::SassTemplate
- Tilt::ScssTemplate
- Tilt::StringTemplate
- Tilt::Template
- Tilt::WikiClothTemplate
- Tilt::YajlTemplate

No matching classes.

# 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)

### Public Class Methods

engine_initialized?() click to toggle source

    # File lib/tilt/yajl.rb, line 45
    def self.engine_initialized?
      defined? ::Yajl
    end

### Public Instance Methods

decorate(json) click to toggle source

Decorates the `json` input according to given `options`.

json - The json String to decorate. options - The option Hash to customize the behavior.

Returns the decorated String.

    # File lib/tilt/yajl.rb, line 80
    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

evaluate(scope, locals, &block) click to toggle source

    # File lib/tilt/yajl.rb, line 56
    def evaluate(scope, locals, &block)
      decorate super(scope, locals, &block)
    end

initialize_engine() click to toggle source

    # File lib/tilt/yajl.rb, line 49
    def initialize_engine
      require_template_library 'yajl'
    end

precompiled_postamble(locals) click to toggle source

    # File lib/tilt/yajl.rb, line 65
    def precompiled_postamble(locals)
      "Yajl::Encoder.new.encode(json)"
    end

precompiled_preamble(locals) click to toggle source

    # File lib/tilt/yajl.rb, line 60
    def precompiled_preamble(locals)
      return super if locals.include? :json
      "json = {}\n#{super}"
    end

precompiled_template(locals) click to toggle source

    # File lib/tilt/yajl.rb, line 69
    def precompiled_template(locals)
      data.to_str
    end

prepare() click to toggle source

    # File lib/tilt/yajl.rb, line 53
    def prepare
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.