Class: Clot::Yield

Inherits:
Liquid::Tag
  • Object
show all
Includes:
Liquid
Defined in:
lib/clot/yield.rb

Constant Summary collapse

Syntax =
/(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ Yield

Returns a new instance of Yield.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/clot/yield.rb', line 7

def initialize(tag_name, markup, tokens)
  @blocks = []
  @naked_yield = false

  if markup =~ Syntax

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

    markup.scan(TagAttributes) do |key, value|
      @attributes[key] = value
    end
  elsif !( markup =~ /\w/ )
    @naked_yield = true
  else
    raise SyntaxError.new("Syntax error in tag 'yield' - Valid syntax: yield '[template]' (with|for) [object|collection]")
  end

  super
end

Instance Method Details

#render(context) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/clot/yield.rb', line 29

def render(context)
  if @naked_yield
    return context['content_for_layout']
  else
    partial = Liquid::Template.load_theme(context, @template_name)
    #source  = Liquid::Template.file_system.read_template_file( "#{context['controller_name']}/#{context['action_name']}/#{@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