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

Defined Under Namespace

Classes: TagRegistry

Constant Summary collapse

@@file_system =
BlankFileSystem.new

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTemplate

Returns a new instance of Template.



120
121
122
123
# File 'lib/liquid/template.rb', line 120

def initialize
  @rethrow_errors = false
  @resource_limits = ResourceLimits.new(self.class.default_resource_limits)
end

Class Attribute Details

.default_exception_rendererObject

Returns the value of attribute default_exception_renderer.



72
73
74
# File 'lib/liquid/template.rb', line 72

def default_exception_renderer
  @default_exception_renderer
end

.error_modeObject



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

def error_mode
  @error_mode ||= :lax
end

.taint_modeObject



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

def taint_mode
  @taint_mode ||= :lax
end

Instance Attribute Details

#profilerObject (readonly)

Returns the value of attribute profiler.



57
58
59
# File 'lib/liquid/template.rb', line 57

def profiler
  @profiler
end

#resource_limitsObject (readonly)

Returns the value of attribute resource_limits.



17
18
19
# File 'lib/liquid/template.rb', line 17

def resource_limits
  @resource_limits
end

#rootObject

Returns the value of attribute root.



16
17
18
# File 'lib/liquid/template.rb', line 16

def root
  @root
end

#warningsObject (readonly)

Returns the value of attribute warnings.



17
18
19
# File 'lib/liquid/template.rb', line 17

def warnings
  @warnings
end

Class Method Details

.default_resource_limitsObject



107
108
109
# File 'lib/liquid/template.rb', line 107

def default_resource_limits
  @default_resource_limits ||= {}
end

.file_systemObject



77
78
79
# File 'lib/liquid/template.rb', line 77

def file_system
  @@file_system
end

.file_system=(obj) ⇒ Object



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

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

.parse(source, options = {}) ⇒ Object

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



114
115
116
117
# File 'lib/liquid/template.rb', line 114

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

.register_filter(mod) ⇒ Object

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



103
104
105
# File 'lib/liquid/template.rb', line 103

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

.register_tag(name, klass) ⇒ Object



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

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

.tagsObject



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

def tags
  @tags ||= TagRegistry.new
end

Instance Method Details

#assignsObject



141
142
143
# File 'lib/liquid/template.rb', line 141

def assigns
  @assigns ||= {}
end

#errorsObject



149
150
151
# File 'lib/liquid/template.rb', line 149

def errors
  @errors ||= []
end

#instance_assignsObject



145
146
147
# File 'lib/liquid/template.rb', line 145

def instance_assigns
  @instance_assigns ||= {}
end

#parse(source, options = {}) ⇒ Object

Parse source code. Returns self for easy chaining



127
128
129
130
131
132
133
134
135
# 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

#registersObject



137
138
139
# File 'lib/liquid/template.rb', line 137

def registers
  @registers ||= {}
end

#render(*args) ⇒ Object

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


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# 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) ⇒ Object



218
219
220
221
# File 'lib/liquid/template.rb', line 218

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