Class: Arrow::Template::IfDirective

Inherits:
BracketingDirective show all
Includes:
ConditionalDirective
Defined in:
lib/arrow/template/if.rb

Overview

The Arrow::Template::IfDirective class, a derivative of Arrow::Template::BracketingDirective. Instances of this class represent a section of a template that is rendered conditionally.

The formats the directive supports are:

  <?if <name>?>...<?end if?>
  <?if <name>.<methodchain>?>...<?end if?>
  <?if <name> (matches|=~) <regex>?>...<?end if?>
  <?if <name>.<methodchain> (matches|=~) <regex>?>...<?end if?>

Note that this directive does not support all possible Ruby expressions in the conditional, and must have a valid associated identifier (the name bit).

Authors

  • Michael Granger

Please see the file LICENSE in the top-level directory for licensing details.

Constant Summary

Constants included from ConditionalDirective

SVNId, SVNRev

Constants inherited from BracketingDirective

SVNId, SVNRev

Constants inherited from AttributeDirective

SVNId, SVNRev

Constants inherited from Directive

SVNId, SVNRev

Constants inherited from Node

SVNId, SVNRev

Constants included from Arrow::HTMLUtilities

ARRAY_HTML_CONTAINER, HASH_HTML_CONTAINER, HASH_PAIR_HTML, IMMEDIATE_OBJECT_HTML_CONTAINER, IVAR_HTML_FRAGMENT, OBJECT_HTML_CONTAINER, THREAD_DUMP_KEY

Instance Method Summary

Methods included from ConditionalDirective

#evaluate, #is_rendering_node?

Methods inherited from BracketingDirective

#add_to_template, #initialize, #inspect, #is_rendering_node?, #parse_directive_contents, #render_subnodes, #to_a, #to_html

Methods inherited from AttributeDirective

allows_format?, #before_rendering, #build_rendering_proc, #call_methodchain, #initialize, #inspect, #is_rendering_node?, #parse_directive_contents, #render, #to_html

Methods inherited from Directive

create, derivativeDirs, #initialize, #inspect, #parse_directive_contents, #render, #to_html

Methods inherited from Node

#add_to_template, #css_class, #initialize, #inspect, #is_rendering_node?, #render, #to_a, #to_html, #to_s

Methods included from Arrow::HTMLUtilities

#escape_html, #make_html_for_object, #make_object_html_wrapper

Methods inherited from Arrow::Object

deprecate_class_method, deprecate_method, inherited

Methods included from Arrow::Loggable

#log

Constructor Details

This class inherits a constructor from Arrow::Template::BracketingDirective

Instance Method Details

- (Object) render_contents(template, scope) (protected)

Render the contents of the conditional if it evaluates to true, or the nodes after ‘elsif’ or ‘else’ subnodes if their conditions are met.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/arrow/template/if.rb', line 45

def render_contents( template, scope )
  cond = hasBeenTrue = self.evaluate( template, scope )

  nodes = []
  
  # Now splice out the chunk of nodes that should be rendered based on
  # the conditional.
  @subnodes.each do |node|
    case node
    when Arrow::Template::ElsifDirective
      if !hasBeenTrue
        cond = hasBeenTrue = node.evaluate( template, scope )
      else
        cond = false
      end

    when Arrow::Template::ElseDirective
      if !hasBeenTrue
        cond = hasBeenTrue = true
      else
        cond = false
      end

    else
      nodes.push( node ) if cond
    end
  end

  return template.render( nodes, scope )
end