Class: Liquid::Include

Inherits:
Tag
  • Object
show all
Defined in:
lib/liquid/standardtags.rb

Constant Summary collapse

Syntax =
/("[^"]+"|'[^']+')(\s+(with|for)\s+(#{QuotedFragment}+))?/

Instance Attribute Summary

Attributes inherited from Tag

#nodelist

Instance Method Summary collapse

Methods inherited from Tag

#name

Constructor Details

#initialize(markup, tokens) ⇒ Include

Returns a new instance of Include.



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/liquid/standardtags.rb', line 343

def initialize(markup, tokens)
  if markup =~ Syntax
    @template_name = $1[1...-1]
    if $2
      @collection = ($3 == "for")
      @variable = $4
    end
    @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

Instance Method Details

#parse(tokens) ⇒ Object



361
362
363
364
365
# File 'lib/liquid/standardtags.rb', line 361

def parse(tokens)      
  source = Liquid::Template.file_system.read_template_file(@template_name)
  tokens = Liquid::Template.tokenize(source)
  @document = Document.new(tokens)
end

#render(context) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/liquid/standardtags.rb', line 367

def render(context)
  result = ''
  variable = context[@variable]
  context.stack do
    @attributes.each do |key, value|
      context[key] = context[value]
    end
    if @collection 
      variable.each do |item|
        context[@template_name] = item
        result << @document.render(context).to_s
      end
    else 
      if @variable          
        context[@template_name] = variable
      end
      result << @document.render(context).to_s
    end
  end
  result
end