Sha256: 4af733b27ae56d5a8306151e1d89789fd5d1c5bdb06e1c310c1180a096974cf8

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

module Liquor
  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)      
      file_system = context.registers[:file_system] || Liquor::Template.file_system
      source  = file_system.read_template_file(context[@template_name])      
      partial = Liquor::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

2 entries across 2 versions & 1 rubygems

Version Path
liquor-0.1.1 lib/liquor/tags/include.rb
liquor-0.1.0 lib/liquor/tags/include.rb