Class: Mullet::HTML::ForElementRenderer

Inherits:
CommandElementRenderer show all
Defined in:
lib/mullet/html/for_element_renderer.rb

Overview

Renders an element for each item in a collection.

Constant Summary

Constants inherited from ElementRenderer

ElementRenderer::ATTRIBUTE_NAME_MISSING_ERROR, ElementRenderer::ATTRIBUTE_NAME_SEPARATOR, ElementRenderer::ATTRIBUTE_SEPARATOR, ElementRenderer::ATTRIBUTE_SYNTAX_ERROR, ElementRenderer::CONVENIENT_ATTRIBUTE_COMMANDS

Instance Attribute Summary

Attributes inherited from ElementRenderer

#remove_mode

Instance Method Summary collapse

Methods inherited from CommandElementRenderer

#has_command

Methods inherited from ElementRenderer

#add_attribute_command, #add_attribute_commands, #add_attribute_message_command, #add_attribute_message_commands, #configure_attribute_commands, #configure_attribute_message_commands, #configure_commands, #configure_content, #execute_attribute_commands, #has_command, #has_dynamic_content, #render_content, #render_end_tag, #render_start_tag, #should_render_content, #should_render_tag

Methods included from Container

#add_child, #children, #clear_children, #delete_child, #render_children

Constructor Details

#initialize(element, variable_name) ⇒ ForElementRenderer

Constructor

Parameters:

  • element (Element)

    element to render

  • variable_name (String)

    name of variable containing collection



15
16
17
18
# File 'lib/mullet/html/for_element_renderer.rb', line 15

def initialize(element, variable_name)
  super(element)
  @variable_name = variable_name.to_sym()
end

Instance Method Details

#render(render_context) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mullet/html/for_element_renderer.rb', line 28

def render(render_context)
  value = render_context.get_variable_value(@variable_name)
  if value == Scope::NOT_FOUND || value == nil || value == false
    return
  end

  if value.respond_to?(:empty?) && value.empty?()
    return
  end

  if value.respond_to?(:each)
    value.each {|item| render_nested_scope(item, render_context) }
    return
  end

  render_nested_scope(value, render_context)
end

#render_nested_scope(data, render_context) ⇒ Object



22
23
24
25
26
# File 'lib/mullet/html/for_element_renderer.rb', line 22

def render_nested_scope(data, render_context)
  render_context.push_scope(data)
  super_render(render_context)
  render_context.pop_scope()
end

#super_renderObject



20
# File 'lib/mullet/html/for_element_renderer.rb', line 20

alias :super_render :render