Class: Inversion::Template::UnlessTag

Inherits:
AttrTag show all
Includes:
ContainerTag
Defined in:
lib/inversion/template/unlesstag.rb

Overview

Inversion ‘unless’ tag.

This tag causes a section of the template to be rendered only if its methodchain or attribute is a false value.

Syntax

<?unless attr ?>...<?end?>
<?unless obj.method ?>...<?end?>

Constant Summary

Constants inherited from Tag

Tag::TAG_PLUGIN_PATTERN

Instance Attribute Summary collapse

Attributes included from ContainerTag

#subnodes

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 included from ContainerTag

#<<, #is_container?, #render_subnodes

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, #before_appending, #before_rendering, #is_container?, #location

Constructor Details

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

Create a new UnlessTag.



40
41
42
43
# File 'lib/inversion/template/unlesstag.rb', line 40

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.



46
47
48
# File 'lib/inversion/template/unlesstag.rb', line 46

def inverted
  @inverted
end

Instance Method Details

#render(state) ⇒ Object

Render the tag’s contents if the condition is true, or any else or elsif sections if the condition isn’t true.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/inversion/template/unlesstag.rb', line 51

def render( state )

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

	# Start out with rendering *disabled* if the tag body evaluates trueishly
	if evaluated_state
		self.log.debug "Initial state was TRUE; disabling rendering"
		state.disable_rendering
	else
		self.log.debug "Initial state was FALSE; enabling rendering"
		state.enable_rendering
	end

	# Set the tag state to track whether or not rendering has been enabled during the
	# 'unless' for an 'else' tag.
	state.with_tag_data( :rendering_was_enabled => state.rendering_enabled? ) do
		self.render_subnodes( state )
	end

	state.enable_rendering
	return nil
end