Class: Luoma::IfTag

Inherits:
Markup show all
Defined in:
lib/luoma/tags/if.rb,
sig/luoma/tags/if.rbs

Constant Summary collapse

END_IF_BLOCK =

Returns:

  • (Set[String])
Set["else", "elif", "elsif", "endif"]
ELSE_IF_TAGS =

Returns:

  • (Set[String])
Set["elif", "elsif"]

Instance Attribute Summary

Attributes inherited from Markup

#blank, #tag_name, #token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Markup

#block_scope, #expressions, #partial, #template_scope

Constructor Details

#initialize(token, tag_name, alts) ⇒ IfTag

Returns a new instance of IfTag.

Parameters:



39
40
41
42
43
44
45
# File 'lib/luoma/tags/if.rb', line 39

def initialize(token, tag_name, alts)
  super(token)
  @tag_name = tag_name
  @alts = alts
  @blank = alts.all?(&:blank)
  @alts.each(&:filter_strings) if @blank
end

Class Method Details

.parse(token, tag_name, parser) ⇒ Markup

(t_token, String, Parser) -> Markup

Parameters:

  • token (t_token)
  • tag_name (String)
  • parser (Parser)

Returns:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/luoma/tags/if.rb', line 9

def self.parse(token, tag_name, parser)
  blocks = [] #: Array[IfBlock | ElseBlock]
  parser.expect_expression
  expression = parser.parse_expression
  parser.carry_whitespace_control
  parser.eat(:token_tag_end)

  block = parser.parse_block(stop: END_IF_BLOCK)
  blocks << IfBlock.new(token, "if", expression, block)

  loop do
    inner_tag_name = parser.tags(ELSE_IF_TAGS)
    break unless inner_tag_name

    blocks << IfBlock.parse(inner_tag_name, parser)
  end

  if parser.tag?("else")
    name_token = parser.eat_empty_tag("else")
    blocks << ElseBlock.new(
      name_token,
      "else",
      parser.parse_block(stop: END_IF_BLOCK)
    )
  end

  parser.eat_empty_tag("endif")
  IfTag.new(token, tag_name, blocks)
end

Instance Method Details

#children(static_context) ⇒ Array[Markup]

(RenderContext) -> Array

Parameters:

Returns:



60
61
62
# File 'lib/luoma/tags/if.rb', line 60

def children(static_context)
  @alts
end

#render(context, buffer) ⇒ void

This method returns an undefined value.

(RenderContext, String) -> void

Parameters:



48
49
50
51
52
53
54
55
56
57
# File 'lib/luoma/tags/if.rb', line 48

def render(context, buffer)
  index = 0
  while (alt = @alts[index])
    if context.env.truthy?(alt.expression.evaluate(context), context)
      Luoma.render_block(alt.block, context, buffer)
      break
    end
    index += 1
  end
end