Class: Inversion::Template::ElseTag

Inherits:
Tag
  • Object
show all
Defined in:
lib/inversion/template/elsetag.rb

Overview

Inversion ‘else’ tag.

This tag adds a logical switch to an IfTag. If the IfTag’s condition was false, start rendering.

Syntax

<?if attr ?>
    ...
<?else ?>
    ...
<?end?>

Constant Summary

Constants inherited from Tag

Tag::TAG_PLUGIN_PATTERN

Instance Attribute Summary

Attributes inherited from Tag

#body

Attributes inherited from Node

#colnum, #linenum

Instance Method Summary collapse

Methods inherited from Tag

#as_comment_body, create, #derivatives, inherited, load, load_all, #tagname, types, #types

Methods included from MethodUtilities

#singleton_attr_accessor, #singleton_attr_reader, #singleton_attr_writer

Methods included from AbstractClass

included

Methods included from AbstractClass::ClassMethods

#inherited, #pure_virtual

Methods inherited from Node

#after_appending, #after_rendering, #as_comment_body, #is_container?, #location

Constructor Details

#initialize(body = '', linenum = nil, colnum = nil) ⇒ ElseTag

Overridden to default body to nothing, and raise an error if it has one.



25
26
27
28
# File 'lib/inversion/template/elsetag.rb', line 25

def initialize( body='', linenum=nil, colnum=nil ) # :notnew:
  raise Inversion::ParseError, "else can't have a condition" unless body.to_s.strip == ''
  super
end

Instance Method Details

#before_appending(parsestate) ⇒ Object

Parsing callback – check to be sure the node tree can have an ‘else’ tag appended to it.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/inversion/template/elsetag.rb', line 38

def before_appending( parsestate )
  condtag = parsestate.node_stack.reverse.find do |node|
    case node

    # If there was a previous 'if' or 'unless', the else belongs to it. Also
    # allow it to be appended to a 'comment' section so you can comment out an
    # else clause
    when Inversion::Template::IfTag,
         Inversion::Template::UnlessTag,
         Inversion::Template::CommentTag
      break node

    # If it's some other kind of container, it's an error
    when Inversion::Template::ContainerTag
      raise Inversion::ParseError, "'%s' tags can't have '%s' clauses" %
        [ node.tagname.downcase, self.tagname.downcase ]
    end
  end

  # If there wasn't a valid container, it's an error too
  raise Inversion::ParseError, "orphaned '%s' tag" % [ self.tagname.downcase ] unless condtag
end

#before_rendering(renderstate) ⇒ Object

Toggle rendering for the iftag’s container if rendering hasn’t yet been toggled.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/inversion/template/elsetag.rb', line 70

def before_rendering( renderstate )
  if renderstate.tag_data[ :rendering_was_enabled ]
    self.log.debug "  rendering was previously enabled: disabling"
    renderstate.disable_rendering
  else
    self.log.debug "  rendering was previously disabled: enabling"
    renderstate.tag_data[ :rendering_was_enabled ] = true
    renderstate.enable_rendering
  end

  return nil
end

#renderObject

Always remder as an empty string.



63
64
65
# File 'lib/inversion/template/elsetag.rb', line 63

def render( * )
  nil
end