Class: Liquid::Case

Inherits:
Block show all
Defined in:
lib/liquid/tags/case.rb

Constant Summary collapse

Syntax =
/(#{QuotedFragment})/
WhenSyntax =
/(#{QuotedFragment})(?:(?:\s+or\s+|\s*\,\s*)(#{QuotedFragment}.*))?/

Constants inherited from Block

Block::ContentOfVariable, Block::FullToken, Block::IsTag, Block::IsVariable

Instance Attribute Summary

Attributes inherited from Tag

#nodelist

Instance Method Summary collapse

Methods inherited from Block

#block_delimiter, #block_name, #create_variable, #end_tag, #parse

Methods inherited from Tag

#name, #parse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ Case

Returns a new instance of Case.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/liquid/tags/case.rb', line 6

def initialize(tag_name, markup, tokens)      
  @blocks = []
  
  if markup =~ Syntax
    @left = $1
  else
    raise SyntaxError.new("Syntax Error in tag 'case' - Valid syntax: case [condition]")
  end
        
  super
end

Instance Method Details

#render(context) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/liquid/tags/case.rb', line 30

def render(context)      
  context.stack do          
    execute_else_block = true
    
    output = ''
    @blocks.each do |block|
      if block.else? 
        return render_all(block.attachment, context) if execute_else_block
      elsif block.evaluate(context)
        execute_else_block = false        
        output << render_all(block.attachment, context)
      end            
    end
    output
  end          
end

#unknown_tag(tag, markup, tokens) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/liquid/tags/case.rb', line 18

def unknown_tag(tag, markup, tokens)
  @nodelist = []
  case tag
  when 'when'
    record_when_condition(markup)
  when 'else'
    record_else_condition(markup)
  else
    super
  end
end