Sha256: e9ae9a46e137103c66d7bd024dd008db12598e9e23b8ba0b538b4ddf11c8c46a

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true
require_relative 'template'
require 'liquid'

module Tilt
  # Liquid template implementation. See:
  # http://liquidmarkup.org/
  #
  # Liquid is designed to be a *safe* template system and therefore
  # does not provide direct access to execuatable scopes. In order to
  # support a +scope+, the +scope+ must be able to represent itself
  # as a hash by responding to #to_h. If the +scope+ does not respond
  # to #to_h it will be ignored.
  #
  # LiquidTemplate does not support yield blocks.
  class LiquidTemplate < Template
    def prepare
      @options[:line_numbers] = true unless @options.has_key?(:line_numbers)
      @engine = ::Liquid::Template.parse(@data, @options)
    end

    def evaluate(scope, locs)
      locals = {}
      if scope.respond_to?(:to_h)
        scope.to_h.each{|k, v| locals[k.to_s] = v}
      end
      locs.each{|k, v| locals[k.to_s] = v}
      locals['yield'] = block_given? ? yield : ''
      locals['content'] = locals['yield']
      @engine.render(locals)
    end

    def allows_script?
      false
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
tilt-2.6.0 lib/tilt/liquid.rb
brakeman-7.0.0 bundle/ruby/3.1.0/gems/tilt-2.5.0/lib/tilt/liquid.rb
tilt-2.5.0 lib/tilt/liquid.rb