Class: Tilt::LiquidTemplate

Inherits:
Template show all
Defined in:
lib/sinatra/tilt.rb

Overview

Liquid template implementation. See: liquid.rubyforge.org/

Liquid is designed to be a safe template system and threfore does not provide direct access to execuatable scopes. In order to support a scope, the scope must be able to represent itself as a hash by responding to #to_h. If the scope does not respond to #to_h it will be ignored.

LiquidTemplate does not support yield blocks.

It’s suggested that your program require ‘liquid’ at load time when using this template engine.

Instance Attribute Summary

Attributes inherited from Template

#data, #file, #line, #options

Instance Method Summary collapse

Methods inherited from Template

#basename, #compile, #eval_file, #initialize, #name, #render

Constructor Details

This class inherits a constructor from Tilt::Template

Instance Method Details

#compile!Object



374
375
376
# File 'lib/sinatra/tilt.rb', line 374

def compile!
  @engine = ::Liquid::Template.parse(data)
end

#evaluate(scope, locals, &block) ⇒ Object



378
379
380
381
382
383
384
385
386
387
388
# File 'lib/sinatra/tilt.rb', line 378

def evaluate(scope, locals, &block)
  locals = locals.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
  if scope.respond_to?(:to_h)
    scope  = scope.to_h.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
    locals = scope.merge(locals)
  end
  # TODO: Is it possible to lazy yield ?
  locals['yield'] = block.nil? ? '' : yield
  locals['content'] = block.nil? ? '' : yield
  @engine.render(locals)
end

#initialize_engineObject



370
371
372
# File 'lib/sinatra/tilt.rb', line 370

def initialize_engine
  require_template_library 'liquid' unless defined? ::Liquid::Template
end