Class: EachProcessor

Inherits:
Object
  • Object
show all
Includes:
Thymeleaf::Processor
Defined in:
lib/thymeleaf/dialects/default/processors/each.rb

Instance Method Summary collapse

Methods included from Thymeleaf::Processor

#evaluate_in_context, #load_template, #subprocess_node

Instance Method Details

#call(node: nil, attribute: nil, context: nil, **_) ⇒ Object



6
7
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
33
34
35
36
37
38
39
40
41
42
# File 'lib/thymeleaf/dialects/default/processors/each.rb', line 6

def call(node:nil, attribute:nil, context:nil, **_)
  variable, stat, enumerable = EachExpression.parse(context, attribute.value)
  
  elements = evaluate_in_context(context, enumerable)
  
  stat_var = init_stat_var(stat, elements)


  attribute.unlink
  
  elements.each do |element|
    subcontext_vars = {}
    subcontext_vars[variable] = element unless variable.nil?

    unless stat.nil?
      stat_var[:index]  += 1
      stat_var[:count]  += 1
      stat_var[:current] = element
      stat_var[:even]    = stat_var[:count].even?
      stat_var[:odd]     = stat_var[:count].odd?
      stat_var[:first]   = (stat_var[:index].eql? 0)
      stat_var[:last]    = (stat_var[:count].eql? stat_var[:size])

      subcontext_vars[stat] = stat_var
    end
    
    subcontext = ContextHolder.new(subcontext_vars, context)
    new_node = node.dup
    subprocess_node(subcontext, new_node)
    node.add_previous_sibling(new_node)
  end

  node.children.each {|child| child.unlink }
  node.unlink
  
  context # TODO: Remove
end

#has_subcontext?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/thymeleaf/dialects/default/processors/each.rb', line 44

def has_subcontext?
  true
end