Class: Inversion::Template::ElsifTag

Inherits:
AttrTag show all
Defined in:
lib/inversion/template/elsiftag.rb

Overview

Inversion ‘elsif’ tag.

This tag adds a conditional logical switch to an IfTag. If the IfTag’s condition was false, but the attribute or methodchain of the elsif is true, start rendering.

Syntax

<?if attr ?>
    ...
<?elsif attr ?>
    ...
<?elsif attr.methodchain ?>
    ...
<?end?>

Constant Summary

Constants inherited from Tag

Tag::TAG_PLUGIN_PATTERN

Instance Attribute Summary collapse

Attributes inherited from AttrTag

#format, #methodchain, #name

Attributes inherited from CodeTag

#body, #identifiers

Attributes inherited from Tag

#body

Attributes inherited from Node

#colnum, #linenum

Instance Method Summary collapse

Methods inherited from AttrTag

#as_comment_body, #evaluate

Methods inherited from CodeTag

inherit_tag_patterns, tag_pattern, tag_patterns

Methods included from AbstractClass

included

Methods included from AbstractClass::ClassMethods

#inherited, #pure_virtual

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 inherited from Node

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

Constructor Details

#initialize(body, linenum = nil, colnum = nil) ⇒ ElsifTag

Create a new ElsifTag.



44
45
46
47
# File 'lib/inversion/template/elsiftag.rb', line 44

def initialize( body, linenum=nil, colnum=nil )
	@inverted = false
	super
end

Instance Attribute Details

#invertedObject

Invert the tag’s renderstate if created with the ‘not’ operator.



50
51
52
# File 'lib/inversion/template/elsiftag.rb', line 50

def inverted
  @inverted
end

Instance Method Details

#before_appending(parsestate) ⇒ Object

Parsing callback – check to be sure the node tree can have an ‘elsif’ tag appended to it (i.e., it has an opening ‘if’ tag).



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/inversion/template/elsiftag.rb', line 55

def before_appending( parsestate )
	condtag = parsestate.node_stack.reverse.find do |node|
		case node
		when Inversion::Template::IfTag,
		     Inversion::Template::CommentTag
			break node
		when Inversion::Template::ContainerTag
			raise Inversion::ParseError, "'%s' tags can't have '%s' clauses" %
				[ node.tagname.downcase, self.tagname.downcase ]
		end
	end

	unless condtag
		raise Inversion::ParseError, "orphaned '%s' tag" % [ self.tagname.downcase ]
	end
end

#before_rendering(renderstate) ⇒ Object

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/inversion/template/elsiftag.rb', line 81

def before_rendering( renderstate )

	evaluated_state = self.evaluate( renderstate )
	evaluated_state = ! evaluated_state if self.inverted

	if renderstate.tag_data[ :rendering_was_enabled ]
		self.log.debug "Rendering was previously enabled; disabling"
		renderstate.disable_rendering
	elsif evaluated_state
		self.log.debug "Rendering was previously disabled, and condition is true; enabling"
		renderstate.tag_data[ :rendering_was_enabled ] = true
		renderstate.enable_rendering
	end

	return nil
end

#renderObject

Always render as an empty string.



74
75
76
# File 'lib/inversion/template/elsiftag.rb', line 74

def render( * )
	nil
end