Class: Liquid::Case

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

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

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.



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

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 Method Details

#nodelistObject



24
25
26
# File 'lib/liquid/tags/case.rb', line 24

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

#parse(tokens) ⇒ Object



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

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

#render(context) ⇒ Object



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

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



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

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