Class: Liquid::Template
- Inherits:
-
Object
- Object
- Liquid::Template
- 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
- DEFAULT_OPTIONS =
{ :locale => I18n.new }
- @@file_system =
BlankFileSystem.new
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#profiler ⇒ Object
readonly
Returns the value of attribute profiler.
-
#resource_limits ⇒ Object
Returns the value of attribute resource_limits.
-
#root ⇒ Object
Returns the value of attribute root.
Class Method Summary collapse
- .default_resource_limits ⇒ Object
- .file_system ⇒ Object
- .file_system=(obj) ⇒ Object
-
.parse(source, options = {}) ⇒ Object
creates a new
Template
object from liquid source code To enable profiling, pass inprofile: true
as an option. -
.register_filter(mod) ⇒ Object
Pass a module with filter methods which should be available to all liquid views.
- .register_tag(name, klass) ⇒ Object
- .tags ⇒ Object
Instance Method Summary collapse
- #assigns ⇒ Object
- #errors ⇒ Object
-
#initialize ⇒ Template
constructor
A new instance of Template.
- #instance_assigns ⇒ Object
-
#parse(source, options = {}) ⇒ Object
Parse source code.
- #registers ⇒ Object
-
#render(*args) ⇒ Object
Render takes a hash with local variables.
- #render!(*args) ⇒ Object
- #warnings ⇒ Object
Constructor Details
#initialize ⇒ Template
Returns a new instance of Template.
112 113 114 |
# File 'lib/liquid/template.rb', line 112 def initialize @resource_limits = self.class.default_resource_limits.dup end |
Class Attribute Details
.error_mode ⇒ Object
85 86 87 |
# File 'lib/liquid/template.rb', line 85 def error_mode @error_mode || :lax end |
.taint_mode ⇒ Object
89 90 91 |
# File 'lib/liquid/template.rb', line 89 def taint_mode @taint_mode || :lax end |
Instance Attribute Details
#profiler ⇒ Object (readonly)
Returns the value of attribute profiler.
54 55 56 |
# File 'lib/liquid/template.rb', line 54 def profiler @profiler end |
#resource_limits ⇒ Object
Returns the value of attribute resource_limits.
21 22 23 |
# File 'lib/liquid/template.rb', line 21 def resource_limits @resource_limits end |
#root ⇒ Object
Returns the value of attribute root.
21 22 23 |
# File 'lib/liquid/template.rb', line 21 def root @root end |
Class Method Details
.default_resource_limits ⇒ Object
99 100 101 |
# File 'lib/liquid/template.rb', line 99 def default_resource_limits @default_resource_limits ||= {} end |
.file_system ⇒ Object
69 70 71 |
# File 'lib/liquid/template.rb', line 69 def file_system @@file_system end |
.file_system=(obj) ⇒ Object
73 74 75 |
# File 'lib/liquid/template.rb', line 73 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
106 107 108 109 |
# File 'lib/liquid/template.rb', line 106 def parse(source, = {}) template = Template.new template.parse(source, ) 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
95 96 97 |
# File 'lib/liquid/template.rb', line 95 def register_filter(mod) Strainer.global_filter(mod) end |
.register_tag(name, klass) ⇒ Object
77 78 79 |
# File 'lib/liquid/template.rb', line 77 def register_tag(name, klass) [name.to_s] = klass end |
.tags ⇒ Object
81 82 83 |
# File 'lib/liquid/template.rb', line 81 def @tags ||= TagRegistry.new end |
Instance Method Details
#assigns ⇒ Object
136 137 138 |
# File 'lib/liquid/template.rb', line 136 def assigns @assigns ||= {} end |
#errors ⇒ Object
144 145 146 |
# File 'lib/liquid/template.rb', line 144 def errors @errors ||= [] end |
#instance_assigns ⇒ Object
140 141 142 |
# File 'lib/liquid/template.rb', line 140 def instance_assigns @instance_assigns ||= {} end |
#parse(source, options = {}) ⇒ Object
Parse source code. Returns self for easy chaining
118 119 120 121 122 123 124 125 |
# File 'lib/liquid/template.rb', line 118 def parse(source, = {}) @options = @profiling = [:profile] @line_numbers = [:line_numbers] || @profiling @root = Document.parse(tokenize(source), DEFAULT_OPTIONS.merge()) @warnings = nil self end |
#registers ⇒ Object
132 133 134 |
# File 'lib/liquid/template.rb', line 132 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
162 163 164 165 166 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 217 218 |
# File 'lib/liquid/template.rb', line 162 def render(*args) return ''.freeze if @root.nil? context = case args.first when Liquid::Context c = args.shift if @rethrow_errors c.exception_handler = ->(e) { true } 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 = args.pop if [:registers].is_a?(Hash) self.registers.merge!([:registers]) end if [:filters] context.add_filters([:filters]) end if [:exception_handler] context.exception_handler = [:exception_handler] 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 = with_profiling 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
220 221 222 223 |
# File 'lib/liquid/template.rb', line 220 def render!(*args) @rethrow_errors = true render(*args) end |
#warnings ⇒ Object
127 128 129 130 |
# File 'lib/liquid/template.rb', line 127 def warnings return [] unless @root @warnings ||= @root.warnings end |