class 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

default_exception_renderer[RW]
error_mode[W]

Sets how strict the parser should be. :lax acts like liquid 2.5 and silently ignores malformed tags in most cases. :warn is the default and will give deprecation warnings when invalid syntax is used. :strict will enforce correct syntax.

taint_mode[W]

Sets how strict the taint checker should be. :lax is the default, and ignores the taint flag completely :warn adds a warning, but does not interrupt the rendering :error raises an error when tainted output is used

profiler[R]
resource_limits[R]
root[RW]
warnings[R]

Public Class Methods

default_resource_limits() click to toggle source
# File lib/liquid/template.rb, line 107
def default_resource_limits
  @default_resource_limits ||= {}
end
error_mode() click to toggle source
# File lib/liquid/template.rb, line 93
def error_mode
  @error_mode ||= :lax
end
file_system() click to toggle source
# File lib/liquid/template.rb, line 77
def file_system
  @@file_system
end
file_system=(obj) click to toggle source
# File lib/liquid/template.rb, line 81
def file_system=(obj)
  @@file_system = obj
end
new() click to toggle source
# File lib/liquid/template.rb, line 120
def initialize
  @rethrow_errors = false
  @resource_limits = ResourceLimits.new(self.class.default_resource_limits)
end
parse(source, options = {}) click to toggle source

creates a new Template object from liquid source code To enable profiling, pass in profile: true as an option. See Liquid::Profiler for more information

# File lib/liquid/template.rb, line 114
def parse(source, options = {})
  template = Template.new
  template.parse(source, options)
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 103
def register_filter(mod)
  Strainer.global_filter(mod)
end
register_tag(name, klass) click to toggle source
# File lib/liquid/template.rb, line 85
def register_tag(name, klass)
  tags[name.to_s] = klass
end
tags() click to toggle source
# File lib/liquid/template.rb, line 89
def tags
  @tags ||= TagRegistry.new
end
taint_mode() click to toggle source
# File lib/liquid/template.rb, line 97
def taint_mode
  @taint_mode ||= :lax
end

Public Instance Methods

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

Parse source code. Returns self for easy chaining

# File lib/liquid/template.rb, line 127
def parse(source, options = {})
  @options = options
  @profiling = options[:profile]
  @line_numbers = options[:line_numbers] || @profiling
  parse_context = options.is_a?(ParseContext) ? options : ParseContext.new(options)
  @root = Document.parse(tokenize(source), parse_context)
  @warnings = parse_context.warnings
  self
end
registers() click to toggle source
# File lib/liquid/template.rb, line 137
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

if profiling was enabled in Template#parse then the resulting profiling information will be available via Template#profiler

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 167
def render(*args)
  return ''.freeze if @root.nil?

  context = case args.first
  when Liquid::Context
    c = args.shift

    if @rethrow_errors
      c.exception_renderer = ->(e) { raise }
    end

    c
  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, "Expected Hash or Liquid::Context as parameter"
  end

  case args.last
  when Hash
    options = args.pop

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

    apply_options_to_context(context, options)
  when Module, Array
    context.add_filters(args.pop)
  end

  # Retrying a render resets resource usage
  context.resource_limits.reset

  begin
    # render the nodelist.
    # for performance reasons we get an array back here. join will make a string out of it.
    result = with_profiling(context) do
      @root.render(context)
    end
    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 218
def render!(*args)
  @rethrow_errors = true
  render(*args)
end

Private Instance Methods

apply_options_to_context(context, options) click to toggle source
# File lib/liquid/template.rb, line 246
def apply_options_to_context(context, options)
  context.add_filters(options[:filters]) if options[:filters]
  context.global_filter = options[:global_filter] if options[:global_filter]
  context.exception_renderer = options[:exception_renderer] if options[:exception_renderer]
  context.strict_variables = options[:strict_variables] if options[:strict_variables]
  context.strict_filters = options[:strict_filters] if options[:strict_filters]
end
tokenize(source) click to toggle source
# File lib/liquid/template.rb, line 225
def tokenize(source)
  Tokenizer.new(source, @line_numbers)
end
with_profiling(context) { || ... } click to toggle source
# File lib/liquid/template.rb, line 229
def with_profiling(context)
  if @profiling && !context.partial
    raise "Profiler not loaded, require 'liquid/profiler' first" unless defined?(Liquid::Profiler)

    @profiler = Profiler.new
    @profiler.start

    begin
      yield
    ensure
      @profiler.stop
    end
  else
    yield
  end
end