Class: Liquid::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid/template.rb

Overview

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

Constant Summary

DEFAULT_OPTIONS =
{
  :locale => I18n.new
}
@@file_system =
BlankFileSystem.new

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Template) initialize

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



68
69
70
# File 'lib/liquid/template.rb', line 68

def initialize
  @resource_limits = {}
end

Instance Attribute Details

- (Object) resource_limits

Returns the value of attribute resource_limits



21
22
23
# File 'lib/liquid/template.rb', line 21

def resource_limits
  @resource_limits
end

- (Object) root

Returns the value of attribute root



21
22
23
# File 'lib/liquid/template.rb', line 21

def root
  @root
end

Class Method Details

+ (Object) error_mode



49
50
51
# File 'lib/liquid/template.rb', line 49

def error_mode
  @error_mode || :lax
end

+ (Object) error_mode=(mode)

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.



45
46
47
# File 'lib/liquid/template.rb', line 45

def error_mode=(mode)
  @error_mode = mode
end

+ (Object) file_system



25
26
27
# File 'lib/liquid/template.rb', line 25

def file_system
  @@file_system
end

+ (Object) file_system=(obj)



29
30
31
# File 'lib/liquid/template.rb', line 29

def file_system=(obj)
  @@file_system = obj
end

+ (Object) parse(source, options = {})

creates a new Template object from liquid source code



60
61
62
63
64
# File 'lib/liquid/template.rb', line 60

def parse(source, options = {})
  template = Template.new
  template.parse(source, options)
  template
end

+ (Object) register_filter(mod)

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



55
56
57
# File 'lib/liquid/template.rb', line 55

def register_filter(mod)
  Strainer.global_filter(mod)
end

+ (Object) register_tag(name, klass)



33
34
35
# File 'lib/liquid/template.rb', line 33

def register_tag(name, klass)
  tags[name.to_s] = klass
end

+ (Object) tags



37
38
39
# File 'lib/liquid/template.rb', line 37

def tags
  @tags ||= {}
end

Instance Method Details

- (Object) assigns



89
90
91
# File 'lib/liquid/template.rb', line 89

def assigns
  @assigns ||= {}
end

- (Object) errors



97
98
99
# File 'lib/liquid/template.rb', line 97

def errors
  @errors ||= []
end

- (Object) instance_assigns



93
94
95
# File 'lib/liquid/template.rb', line 93

def instance_assigns
  @instance_assigns ||= {}
end

- (Object) parse(source, options = {})

Parse source code. Returns self for easy chaining



74
75
76
77
78
# File 'lib/liquid/template.rb', line 74

def parse(source, options = {})
  @root = Document.parse(tokenize(source), DEFAULT_OPTIONS.merge(options))
  @warnings = nil
  self
end

- (Object) registers



85
86
87
# File 'lib/liquid/template.rb', line 85

def registers
  @registers ||= {}
end

- (Object) render(*args)

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


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/liquid/template.rb', line 112

def render(*args)
  return ''.freeze 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, "Expected 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

- (Object) render!(*args)



159
160
161
# File 'lib/liquid/template.rb', line 159

def render!(*args)
  @rethrow_errors = true; render(*args)
end

- (Object) warnings



80
81
82
83
# File 'lib/liquid/template.rb', line 80

def warnings
  return [] unless @root
  @warnings ||= @root.warnings
end