Sha256: cd8f9a0ac6c0a1d495771812b1de1ed4b1192b467c42a862767c033be8f99631

Contents?: true

Size: 1.36 KB

Versions: 7

Compression:

Stored size: 1.36 KB

Contents

module Liquid
  class Include < Tag
    Syntax = /(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?/
  
    def initialize(tag_name, markup, tokens)      
      if markup =~ Syntax

        @template_name = $1        
        @variable_name = $3
        @attributes    = {}

        markup.scan(TagAttributes) do |key, value|
          @attributes[key] = value
        end

      else
        raise SyntaxError.new("Error in tag 'include' - Valid syntax: include '[template]' (with|for) [object|collection]")
      end

      super
    end
  
    def parse(tokens)      
    end
  
    def render(context)      
      source  = Liquid::Template.file_system.read_template_file(context[@template_name])      
      partial = Liquid::Template.parse(source)      
      
      variable = context[@variable_name || @template_name[1..-2]]
      
      context.stack do
        @attributes.each do |key, value|
          context[key] = context[value]
        end

        if variable.is_a?(Array)
          
          variable.collect do |variable|            
            context[@template_name[1..-2]] = variable
            partial.render(context)
          end

        else
                    
          context[@template_name[1..-2]] = variable
          partial.render(context)
          
        end
      end
    end
  end

  Template.register_tag('include', Include)  
end

Version data entries

7 entries across 7 versions & 6 rubygems

Version Path
jeremyf-liquid-2.0.2 lib/liquid/tags/include.rb
qoobaa-liquid-2.0.1 lib/liquid/tags/include.rb
tobi-liquid-2.0.1 lib/liquid/tags/include.rb
agilitic-liquid-2.0.1 lib/liquid/tags/include.rb
drnic-liquid-2.1.0 lib/liquid/tags/include.rb
liquid-1.9.0 lib/liquid/tags/include.rb
liquid-2.0.0 lib/liquid/tags/include.rb