Class: Liquid::Template
- Inherits:
-
Object
- Object
- Liquid::Template
- Defined in:
- lib/liquid/template.rb
Overview
Templates are central to liquid. Interpreting 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')
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#profiler ⇒ Object
readonly
Returns the value of attribute profiler.
-
#resource_limits ⇒ Object
readonly
Returns the value of attribute resource_limits.
-
#root ⇒ Object
Returns the value of attribute root.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Class Method Summary collapse
- .default_exception_renderer ⇒ Object
- .default_exception_renderer=(renderer) ⇒ Object
- .default_resource_limits ⇒ Object
- .error_mode ⇒ Object
-
.error_mode=(mode) ⇒ Object
Sets how strict the parser should be.
- .file_system ⇒ Object
- .file_system=(file_system) ⇒ Object
-
.parse(source, options = {}) ⇒ Object
creates a new
Templateobject from liquid source code To enable profiling, pass inprofile: trueas 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(environment: Environment.default) ⇒ 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
- #render_to_output_buffer(context, output) ⇒ Object
Constructor Details
#initialize(environment: Environment.default) ⇒ Template
Returns a new instance of Template.
90 91 92 93 94 |
# File 'lib/liquid/template.rb', line 90 def initialize(environment: Environment.default) @environment = environment @rethrow_errors = false @resource_limits = ResourceLimits.new(environment.default_resource_limits) end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
18 19 20 |
# File 'lib/liquid/template.rb', line 18 def name @name end |
#profiler ⇒ Object (readonly)
Returns the value of attribute profiler.
21 22 23 |
# File 'lib/liquid/template.rb', line 21 def profiler @profiler end |
#resource_limits ⇒ Object (readonly)
Returns the value of attribute resource_limits.
19 20 21 |
# File 'lib/liquid/template.rb', line 19 def resource_limits @resource_limits end |
#root ⇒ Object
Returns the value of attribute root.
18 19 20 |
# File 'lib/liquid/template.rb', line 18 def root @root end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
19 20 21 |
# File 'lib/liquid/template.rb', line 19 def warnings @warnings end |
Class Method Details
.default_exception_renderer ⇒ Object
43 44 45 |
# File 'lib/liquid/template.rb', line 43 def default_exception_renderer Environment.default.exception_renderer end |
.default_exception_renderer=(renderer) ⇒ Object
38 39 40 41 |
# File 'lib/liquid/template.rb', line 38 def default_exception_renderer=(renderer) Deprecations.warn("Template.default_exception_renderer=", "Environment#exception_renderer=") Environment.default.exception_renderer = renderer end |
.default_resource_limits ⇒ Object
77 78 79 |
# File 'lib/liquid/template.rb', line 77 def default_resource_limits Environment.default.default_resource_limits end |
.error_mode ⇒ Object
34 35 36 |
# File 'lib/liquid/template.rb', line 34 def error_mode Environment.default.error_mode end |
.error_mode=(mode) ⇒ Object
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 enforces correct syntax for most tags :rigid enforces correct syntax for all tags
29 30 31 32 |
# File 'lib/liquid/template.rb', line 29 def error_mode=(mode) Deprecations.warn("Template.error_mode=", "Environment#error_mode=") Environment.default.error_mode = mode end |
.file_system ⇒ Object
52 53 54 |
# File 'lib/liquid/template.rb', line 52 def file_system Environment.default.file_system end |
.file_system=(file_system) ⇒ Object
47 48 49 50 |
# File 'lib/liquid/template.rb', line 47 def file_system=(file_system) Deprecations.warn("Template.file_system=", "Environment#file_system=") Environment.default.file_system = file_system 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
84 85 86 87 |
# File 'lib/liquid/template.rb', line 84 def parse(source, = {}) environment = [:environment] || Environment.default new(environment: environment).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
67 68 69 70 |
# File 'lib/liquid/template.rb', line 67 def register_filter(mod) Deprecations.warn("Template.register_filter", "Environment#register_filter") Environment.default.register_filter(mod) end |
.register_tag(name, klass) ⇒ Object
60 61 62 63 |
# File 'lib/liquid/template.rb', line 60 def register_tag(name, klass) Deprecations.warn("Template.register_tag", "Environment#register_tag") Environment.default.register_tag(name, klass) end |
.tags ⇒ Object
56 57 58 |
# File 'lib/liquid/template.rb', line 56 def Environment.default. end |
Instance Method Details
#assigns ⇒ Object
115 116 117 |
# File 'lib/liquid/template.rb', line 115 def assigns @assigns ||= {} end |
#errors ⇒ Object
123 124 125 |
# File 'lib/liquid/template.rb', line 123 def errors @errors ||= [] end |
#instance_assigns ⇒ Object
119 120 121 |
# File 'lib/liquid/template.rb', line 119 def instance_assigns @instance_assigns ||= {} end |
#parse(source, options = {}) ⇒ Object
Parse source code. Returns self for easy chaining
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/liquid/template.rb', line 98 def parse(source, = {}) parse_context = () source = source.to_s.to_str unless source.valid_encoding? raise TemplateEncodingError, parse_context.locale.t("errors.syntax.invalid_template_encoding") end tokenizer = parse_context.new_tokenizer(source, start_line_number: @line_numbers && 1) @root = Document.parse(tokenizer, parse_context) self end |
#registers ⇒ Object
111 112 113 |
# File 'lib/liquid/template.rb', line 111 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 and might be useful to integrate liquid more with its host application
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 |
# File 'lib/liquid/template.rb', line 141 def render(*args) return '' if @root.nil? context = case args.first when Liquid::Context c = args.shift if @rethrow_errors c.exception_renderer = Liquid::RAISE_EXCEPTION_LAMBDA end c when Liquid::Drop drop = args.shift drop.context = Context.new([drop, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits, {}, @environment) when Hash Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits, {}, @environment) when nil Context.new(assigns, instance_assigns, registers, @rethrow_errors, @resource_limits, {}, @environment) else raise ArgumentError, "Expected Hash or Liquid::Context as parameter" end output = nil case args.last when Hash = args.pop output = [:output] if [:output] static_registers = context.registers.static [:registers]&.each do |key, register| static_registers[key] = register end (context, ) when Module, Array context.add_filters(args.pop) end # Retrying a render resets resource usage context.resource_limits.reset if @profiling && context.profiler.nil? @profiler = context.profiler = Liquid::Profiler.new end context.template_name ||= name begin # render the nodelist. @root.render_to_output_buffer(context, output || +'') rescue Liquid::MemoryError => e context.handle_error(e) ensure @errors = context.errors end end |
#render!(*args) ⇒ Object
200 201 202 203 |
# File 'lib/liquid/template.rb', line 200 def render!(*args) @rethrow_errors = true render(*args) end |
#render_to_output_buffer(context, output) ⇒ Object
205 206 207 |
# File 'lib/liquid/template.rb', line 205 def render_to_output_buffer(context, output) render(context, output: output) end |