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
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.
Public Class Methods
# File lib/liquid-render-tag/template.rb, line 104 def add_register(name, klass) registers[name.to_sym] = klass end
# File lib/liquid-render-tag/template.rb, line 126 def initialize @rethrow_errors = false @resource_limits = ResourceLimits.new(Template.default_resource_limits) end
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-render-tag/template.rb, line 121 def parse(source, options = {}) new.parse(source, options) end
Pass a module with filter methods which should be available to all liquid views. Good for registering the standard library
# File lib/liquid-render-tag/template.rb, line 110 def register_filter(mod) StrainerFactory.add_global_filter(mod) end
# File lib/liquid-render-tag/template.rb, line 96 def register_tag(name, klass) tags[name.to_s] = klass end
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 @deprecated Since it is being deprecated in ruby itself.
# File lib/liquid-render-tag/template.rb, line 74 def taint_mode=(mode) taint_supported = Object.new.taint.tainted? if mode != :lax && !taint_supported raise NotImplementedError, "#{RUBY_ENGINE} #{RUBY_VERSION} doesn't support taint checking" end @taint_mode = mode end
Public Instance Methods
# File lib/liquid-render-tag/template.rb, line 147 def assigns @assigns ||= {} end
# File lib/liquid-render-tag/template.rb, line 155 def errors @errors ||= [] end
# File lib/liquid-render-tag/template.rb, line 151 def instance_assigns @instance_assigns ||= {} end
Parse source code. Returns self for easy chaining
# File lib/liquid-render-tag/template.rb, line 133 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
# File lib/liquid-render-tag/template.rb, line 143 def registers @registers ||= {} end
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-render-tag/template.rb, line 173 def render(*args) return '' 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 output = nil context_register = context.registers.is_a?(StaticRegisters) ? context.registers.static : context.registers case args.last when Hash options = args.pop output = options[:output] if options[:output] options[:registers]&.each do |key, register| context_register[key] = register end apply_options_to_context(context, options) when Module, Array context.add_filters(args.pop) end Template.registers.each do |key, register| context_register[key] = register 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. with_profiling(context) do @root.render_to_output_buffer(context, output || +'') end rescue Liquid::MemoryError => e context.handle_error(e) ensure @errors = context.errors end end
# File lib/liquid-render-tag/template.rb, line 234 def render!(*args) @rethrow_errors = true render(*args) end
# File lib/liquid-render-tag/template.rb, line 239 def render_to_output_buffer(context, output) render(context, output: output) end
Private Instance Methods
# File lib/liquid-render-tag/template.rb, line 266 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
# File lib/liquid-render-tag/template.rb, line 245 def tokenize(source) Tokenizer.new(source, @line_numbers) end
# File lib/liquid-render-tag/template.rb, line 249 def with_profiling(context) if @profiling && !context.partial raise "Profiler not loaded, require 'liquid/profiler' first" unless defined?(Liquid::Profiler) @profiler = Profiler.new(context.template_name) @profiler.start begin yield ensure @profiler.stop end else yield end end