Parent

Liquid::Template

Templates are central to liquid. Interpretating templates is a two step process. First you compile the source code you got. During compile time some extensive error checking is performed. your code should expect to get some SyntaxErrors.

After you have a compiled template you can then render it. You can use a compiled template over and over again and keep it cached.

Example:

template = Liquid::Template.parse(source)
template.render('user_name' => 'bob')

Attributes

resource_limits[RW]
root[RW]

Public Class Methods

file_system() click to toggle source
# File lib/liquid/template.rb, line 21
def file_system
  @@file_system
end
file_system=(obj) click to toggle source
# File lib/liquid/template.rb, line 25
def file_system=(obj)
  @@file_system = obj
end
new() click to toggle source

creates a new Template from an array of tokens. Use Template.parse instead

# File lib/liquid/template.rb, line 52
def initialize
  @resource_limits = {}
end
parse(source) click to toggle source

creates a new Template object from liquid source code

# File lib/liquid/template.rb, line 44
def parse(source)
  template = Template.new
  template.parse(source)
  template
end
register_filter(mod) click to toggle source

Pass a module with filter methods which should be available to all liquid views. Good for registering the standard library

# File lib/liquid/template.rb, line 39
def register_filter(mod)
  Strainer.global_filter(mod)
end
register_tag(name, klass) click to toggle source
# File lib/liquid/template.rb, line 29
def register_tag(name, klass)
  tags[name.to_s] = klass
end
tags() click to toggle source
# File lib/liquid/template.rb, line 33
def tags
  @tags ||= {}
end

Public Instance Methods

assigns() click to toggle source
# File lib/liquid/template.rb, line 67
def assigns
  @assigns ||= {}
end
errors() click to toggle source
# File lib/liquid/template.rb, line 75
def errors
  @errors ||= []
end
instance_assigns() click to toggle source
# File lib/liquid/template.rb, line 71
def instance_assigns
  @instance_assigns ||= {}
end
parse(source) click to toggle source

Parse source code. Returns self for easy chaining

# File lib/liquid/template.rb, line 58
def parse(source)
  @root = Document.new(tokenize(source))
  self
end
registers() click to toggle source
# File lib/liquid/template.rb, line 63
def registers
  @registers ||= {}
end
render(*args) click to toggle source

Render takes a hash with local variables.

if you use the same filters over and over again consider registering them globally with Template.register_filter

Following options can be passed:

* <tt>filters</tt> : array with local filters
* <tt>registers</tt> : hash with register variables. Those can be accessed from
  filters and tags and might be useful to integrate liquid more with its host application
# File lib/liquid/template.rb, line 90
def render(*args)
  return '' if @root.nil?

  context = case args.first
  when Liquid::Context
    args.shift
  when Liquid::Drop
    drop = args.shift
    drop.context = Context.new([drop, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits)
  when Hash
    Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits)
  when nil
    Context.new(assigns, instance_assigns, registers, @rethrow_errors, @resource_limits)
  else
    raise ArgumentError, "Expect Hash or Liquid::Context as parameter"
  end

  case args.last
  when Hash
    options = args.pop

    if options[:registers].is_a?(Hash)
      self.registers.merge!(options[:registers])
    end

    if options[:filters]
      context.add_filters(options[:filters])
    end

  when Module
    context.add_filters(args.pop)
  when Array
    context.add_filters(args.pop)
  end

  begin
    # render the nodelist.
    # for performance reasons we get an array back here. join will make a string out of it.
    result = @root.render(context)
    result.respond_to?(:join) ? result.join : result
  rescue Liquid::MemoryError => e
    context.handle_error(e)
  ensure
    @errors = context.errors
  end
end
render!(*args) click to toggle source
# File lib/liquid/template.rb, line 137
def render!(*args)
  @rethrow_errors = true; render(*args)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.