Class: Liquid2::IfTag

Inherits:
Tag show all
Defined in:
lib/liquid2/nodes/tags/if.rb

Overview

The standard if tag

Direct Known Subclasses

UnlessTag

Constant Summary collapse

END_TAG =
"endif"
END_BLOCK =
Set["else", "elsif", "endif"].freeze

Instance Attribute Summary

Attributes inherited from Node

#blank, #token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tag

#name

Methods inherited from Node

#block_scope, #partial_scope, #render_with_disabled_tag_check, #template_scope

Constructor Details

#initialize(token, expression, block, alternatives, default) ⇒ IfTag

Returns a new instance of IfTag.

Parameters:



54
55
56
57
58
59
60
61
# File 'lib/liquid2/nodes/tags/if.rb', line 54

def initialize(token, expression, block, alternatives, default)
  super(token)
  @expression = expression
  @block = block
  @alternatives = alternatives
  @default = default
  @blank = block.blank && alternatives.all?(&:blank) && (!default || default.blank)
end

Class Method Details

.parse(token, parser) ⇒ IfTag

Parameters:

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/liquid2/nodes/tags/if.rb', line 14

def self.parse(token, parser)
  parser.expect_expression
  expression = BooleanExpression.new(parser.current, parser.parse_primary)
  parser.carry_whitespace_control
  parser.eat(:token_tag_end)

  block = parser.parse_block(self::END_BLOCK)
  alternatives = [] # : Array[ConditionalBlock]
  alternatives << parse_elsif(parser) while parser.tag?("elsif")

  if parser.tag?("else")
    parser.eat_empty_tag("else")
    default = parser.parse_block(self::END_BLOCK)
  else
    default = nil
  end

  parser.eat_empty_tag(self::END_TAG)
  new(token, expression, block, alternatives, default)
end

.parse_elsif(parser) ⇒ ConditionalBlock

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/liquid2/nodes/tags/if.rb', line 36

def self.parse_elsif(parser)
  parser.eat(:token_tag_start)
  parser.skip_whitespace_control
  token = parser.eat(:token_tag_name)
  parser.expect_expression
  expression = BooleanExpression.new(parser.current, parser.parse_primary)
  parser.carry_whitespace_control
  parser.eat(:token_tag_end)

  block = parser.parse_block(self::END_BLOCK)
  ConditionalBlock.new(token, expression, block)
end

Instance Method Details

#children(_static_context, include_partials: true) ⇒ Object



75
76
77
78
79
80
# File 'lib/liquid2/nodes/tags/if.rb', line 75

def children(_static_context, include_partials: true)
  # @type var nodes: Array[Node]
  nodes = [@block, *@alternatives]
  nodes << (@default || raise) if @default
  nodes
end

#expressionsObject



82
# File 'lib/liquid2/nodes/tags/if.rb', line 82

def expressions = [@expression]

#render(context, buffer) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/liquid2/nodes/tags/if.rb', line 63

def render(context, buffer)
  return @block.render(context, buffer) if context.evaluate(@expression)

  index = 0
  while (alt = @alternatives[index])
    index += 1
    return alt.block.render(context, buffer) if context.evaluate(alt.expression)
  end

  (@default || raise).render(context, buffer) if @default
end