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')
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.
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
# File lib/liquid/template.rb, line 99 def default_resource_limits @default_resource_limits ||= {} end
# File lib/liquid/template.rb, line 85 def error_mode @error_mode || :lax end
# File lib/liquid/template.rb, line 69 def file_system @@file_system end
# File lib/liquid/template.rb, line 73 def file_system=(obj) @@file_system = obj end
# File lib/liquid/template.rb, line 112 def initialize @resource_limits = self.class.default_resource_limits.dup 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/template.rb, line 106 def parse(source, options = {}) template = Template.new template.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/template.rb, line 95 def register_filter(mod) Strainer.global_filter(mod) end
# File lib/liquid/template.rb, line 77 def register_tag(name, klass) tags[name.to_s] = klass end
# File lib/liquid/template.rb, line 89 def taint_mode @taint_mode || :lax end
# File lib/liquid/template.rb, line 136 def assigns @assigns ||= {} end
# File lib/liquid/template.rb, line 144 def errors @errors ||= [] end
# File lib/liquid/template.rb, line 140 def instance_assigns @instance_assigns ||= {} end
Parse source code. Returns self for easy chaining
# File lib/liquid/template.rb, line 118 def parse(source, options = {}) @options = options @profiling = options[:profile] @line_numbers = options[:line_numbers] || @profiling @root = Document.parse(tokenize(source), DEFAULT_OPTIONS.merge(options)) @warnings = nil self end
# File lib/liquid/template.rb, line 132 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/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 options = args.pop if options[:registers].is_a?(Hash) self.registers.merge!(options[:registers]) end if options[:filters] context.add_filters(options[:filters]) end if options[:exception_handler] context.exception_handler = options[: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
# File lib/liquid/template.rb, line 220 def render!(*args) @rethrow_errors = true render(*args) end
# File lib/liquid/template.rb, line 127 def warnings return [] unless @root @warnings ||= @root.warnings end
# File lib/liquid/template.rb, line 240 def calculate_line_numbers(raw_tokens) return raw_tokens unless @line_numbers current_line = 1 raw_tokens.map do |token| Token.new(token, current_line).tap do current_line += token.count("\n") end end end
Uses the Liquid::TemplateParser
regexp to tokenize the passed
source
# File lib/liquid/template.rb, line 228 def tokenize(source) source = source.source if source.respond_to?(:source) return [] if source.to_s.empty? tokens = calculate_line_numbers(source.split(TemplateParser)) # removes the rogue empty element at the beginning of the array tokens.shift if tokens[0] and tokens[0].empty? tokens end
# File lib/liquid/template.rb, line 251 def with_profiling if @profiling && !@options[:included] @profiler = Profiler.new @profiler.start begin yield ensure @profiler.stop end else yield end end