Class: Luoma::ForTag

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

Constant Summary collapse

END_FOR_BLOCK =

Returns:

  • (Set[String])
Set["else", "endfor"]

Instance Attribute Summary

Attributes inherited from Markup

#blank, #tag_name, #token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Markup

#partial, #template_scope

Constructor Details

#initialize(token, tag_name, params, expression, block, default) ⇒ Object

Signature:

  • (t_token, String, Array[Name], Expression, t_block, t_block?) -> void



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/luoma/tags/for.rb', line 35

def initialize(token, tag_name, params, expression, block, default)
  super(token)
  @tag_name = tag_name
  @params = params
  @expression = expression

  @blank = Luoma.blank_block?(block) && (!default || Luoma.blank_block?(default))

  @block = if @blank
             block.grep_v(String)
           else
             block
           end

  @default = if blank && default
               default.grep_v(String)
             else
               default
             end
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:



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

def self.parse(token, tag_name, parser)
  params = [parser.parse_ident] #: Array[Name]

  while parser.kind == :token_comma
    parser.next
    params << parser.parse_ident
  end

  parser.eat(:token_in, message: "missing 'in'")
  parser.expect_expression
  expr = parser.parse_expression
  parser.carry_whitespace_control
  parser.eat(:token_tag_end)

  block = parser.parse_block(stop: END_FOR_BLOCK)

  default = if parser.tag?("else")
              parser.eat_empty_tag("else")
              parser.parse_block(stop: END_FOR_BLOCK)
            end

  parser.eat_empty_tag("endfor")

  new(token, tag_name, params, expr, block, default)
end

Instance Method Details

#block_scopeObject

Signature:

  • () -> Array[Name]



104
105
106
# File 'lib/luoma/tags/for.rb', line 104

def block_scope
  @params
end

#children(static_context) ⇒ Object

Signature:

  • (RenderContext) -> Array[Markup]



92
93
94
95
96
# File 'lib/luoma/tags/for.rb', line 92

def children(static_context)
  result = @block.grep_v(String) #: Array[Markup]
  result.concat(@default.grep_v(String)) if @default # steep:ignore
  result
end

#expressionsObject

Signature:

  • () -> Array[Expression]



99
100
101
# File 'lib/luoma/tags/for.rb', line 99

def expressions
  [@expression, @offset, @limit].compact
end

#render(context, buffer) ⇒ Object

Signature:

  • (RenderContext, String) -> void



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/luoma/tags/for.rb', line 57

def render(context, buffer)
  array = context.env.to_a(@expression.evaluate(context), context)

  if array.empty? && @default
    Luoma.render_block(@default || raise, context, buffer)
    return
  end

  namespace = {} #: Hash[String, untyped]
  namespace[@params[2].value] = array if @params.length > 2

  name_param = @params.first.value
  index_param = @params[1].value if @params.length > 1
  length = array.length

  context.extends(namespace) do
    index = 0
    while index < length
      namespace[name_param] = array[index]
      namespace[index_param] = index if index_param
      index += 1

      Luoma.render_block(@block, context, buffer)

      case context.interrupts.pop
      when :continue
        next
      when :break
        break
      end
    end
  end
end