Class: Antlers::IfNode

Inherits:
BranchNode show all
Includes:
Props, Variables
Defined in:
lib/nodes/if_node.rb

Instance Attribute Summary

Attributes included from Props

#props

Attributes inherited from BranchNode

#children

Attributes inherited from AntlerNode

#name

Instance Method Summary collapse

Methods included from Variables

#evaluate, #fallback

Methods included from Queries

user_defined_string?, wrapped_in?

Methods inherited from AntlerNode

#==, #eql?, #hash

Constructor Details

#initialize(name:, value:, props: [], children: []) ⇒ IfNode

Returns a new instance of IfNode.



12
13
14
15
16
# File 'lib/nodes/if_node.rb', line 12

def initialize(name:, value:, props: [], children: [])
  super(name:, props:, children:)

  @value = value
end

Instance Method Details

#render(current_binding: nil, parent_binding: nil, slot_node: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/nodes/if_node.rb', line 18

def render(current_binding: nil, parent_binding: nil, slot_node: nil)
  output = ''.dup

  if evaluate(name: @value, current_binding:)
    @children.each do |child|
      # Antlers nodes respond to "render", whereas HTML is stored as a string and output as is.
      output += (child.respond_to?(:render) ? child.render(current_binding:, parent_binding:, slot_node:) : child) || ''
    end
  end

  output
end