Class: Liquid::Case

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

Defined Under Namespace

Classes: ParseTreeVisitor

Constant Summary collapse

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

Constants inherited from Block

Block::MAX_DEPTH

Instance Attribute Summary collapse

Attributes inherited from Tag

#line_number, #parse_context, #tag_name

Instance Method Summary collapse

Methods inherited from Block

#blank?, #block_delimiter, #block_name

Methods inherited from Tag

#blank?, #name, parse, #raw

Methods included from ParserSwitching

#parse_with_selected_parser

Constructor Details

#initialize(tag_name, markup, options) ⇒ Case

Returns a new instance of Case.



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

def initialize(tag_name, markup, options)
  super
  @blocks = []

  if markup =~ Syntax
    @left = Expression.parse($1)
  else
    raise SyntaxError.new(options[:locale].t("errors.syntax.case".freeze))
  end
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



6
7
8
# File 'lib/liquid/tags/case.rb', line 6

def blocks
  @blocks
end

#leftObject (readonly)

Returns the value of attribute left.



6
7
8
# File 'lib/liquid/tags/case.rb', line 6

def left
  @left
end

Instance Method Details

#nodelistObject



26
27
28
# File 'lib/liquid/tags/case.rb', line 26

def nodelist
  @blocks.map(&:attachment)
end

#parse(tokens) ⇒ Object



19
20
21
22
23
24
# File 'lib/liquid/tags/case.rb', line 19

def parse(tokens)
  body = BlockBody.new
  while parse_body(body, tokens)
    body = @blocks.last.attachment
  end
end

#render(context) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/liquid/tags/case.rb', line 41

def render(context)
  context.stack do
    execute_else_block = true

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

#unknown_tag(tag, markup, tokens) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/liquid/tags/case.rb', line 30

def unknown_tag(tag, markup, tokens)
  case tag
  when 'when'.freeze
    record_when_condition(markup)
  when 'else'.freeze
    record_else_condition(markup)
  else
    super
  end
end