Sha256: 119cbf842b2a2404f7cf301aa0208b964c1c6ab3bc598dc93952c2b9aaf5736d

Contents?: true

Size: 1.21 KB

Versions: 166

Compression:

Stored size: 1.21 KB

Contents

require 'tilt/template'
require 'liquid'

module Tilt
  # Liquid template implementation. See:
  # http://liquidmarkup.org/
  #
  # Liquid is designed to be a *safe* template system and threfore
  # 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.
  #
  # It's suggested that your program require 'liquid' at load
  # time when using this template engine.
  class LiquidTemplate < Template
    def prepare
      @engine = ::Liquid::Template.parse(data, liquid_options)
    end

    def evaluate(scope, locals, &block)
      locals = locals.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
      if scope.respond_to?(:to_h)
        scope  = scope.to_h.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
        locals = scope.merge(locals)
      end
      locals['yield'] = block.nil? ? '' : yield
      locals['content'] = locals['yield']
      @engine.render(locals)
    end

    def allows_script?
      false
    end

    private

    def liquid_options
      { line_numbers: true }.merge options
    end
  end
end

Version data entries

166 entries across 127 versions & 20 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/tilt-2.0.10/lib/tilt/liquid.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/tilt-2.0.10/lib/tilt/liquid.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/tilt-2.0.10/lib/tilt/liquid.rb
brakeman-6.1.2 bundle/ruby/3.3.0/gems/tilt-2.0.11/lib/tilt/liquid.rb
brakeman-6.1.1 bundle/ruby/3.0.0/gems/tilt-2.0.11/lib/tilt/liquid.rb
brakeman-6.1.0 bundle/ruby/3.1.0/gems/tilt-2.0.11/lib/tilt/liquid.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/tilt-2.0.11/lib/tilt/liquid.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/tilt-2.0.10/lib/tilt/liquid.rb
brakeman-6.0.1 bundle/ruby/3.1.0/gems/tilt-2.0.11/lib/tilt/liquid.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/tilt-2.0.11/lib/tilt/liquid.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/tilt-2.0.10/lib/tilt/liquid.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/tilt-2.0.11/lib/tilt/liquid.rb
brakeman-6.0.0 bundle/ruby/3.0.0/gems/tilt-2.0.11/lib/tilt/liquid.rb
brakeman-5.4.1 bundle/ruby/3.1.0/gems/tilt-2.0.11/lib/tilt/liquid.rb
tilt-2.1.0 lib/tilt/liquid.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/tilt-2.0.11/lib/tilt/liquid.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/tilt-2.0.11/lib/tilt/liquid.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/tilt-2.0.11/lib/tilt/liquid.rb
brakeman-5.4.0 bundle/ruby/2.7.0/gems/tilt-2.0.11/lib/tilt/liquid.rb
brakeman-5.3.1 bundle/ruby/2.7.0/gems/tilt-2.0.11/lib/tilt/liquid.rb