Class: Inversion::Template::IfTag
- Includes:
- ContainerTag
- Defined in:
- lib/inversion/template/iftag.rb
Overview
Inversion ‘if’ tag.
This tag causes a section of the template to be rendered only if its methodchain or attribute is a true value.
Syntax
<?if attr ?>...<?end?>
<?if obj.method ?>...<?end?>
Constant Summary
Constants inherited from Tag
Instance Attribute Summary collapse
-
#inverted ⇒ Object
Invert the tag’s renderstate if created with the ‘not’ operator.
Attributes included from ContainerTag
Attributes inherited from AttrTag
Attributes inherited from CodeTag
Attributes inherited from Tag
Attributes inherited from Node
Instance Method Summary collapse
-
#initialize(body, linenum = nil, colnum = nil) ⇒ IfTag
constructor
Create a new IfTag.
-
#render(renderstate) ⇒ Object
Render the tag’s contents if the condition is true, or any else or elsif sections if the condition isn’t true.
Methods included from ContainerTag
#<<, #is_container?, #render_subnodes
Methods inherited from AttrTag
Methods inherited from CodeTag
inherit_tag_patterns, tag_pattern, tag_patterns
Methods included from AbstractClass
Methods included from AbstractClass::ClassMethods
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) ⇒ IfTag
Create a new IfTag.
42 43 44 45 |
# File 'lib/inversion/template/iftag.rb', line 42 def initialize( body, linenum=nil, colnum=nil ) @inverted = false super end |
Instance Attribute Details
#inverted ⇒ Object
Invert the tag’s renderstate if created with the ‘not’ operator.
48 49 50 |
# File 'lib/inversion/template/iftag.rb', line 48 def inverted @inverted end |
Instance Method Details
#render(renderstate) ⇒ Object
Render the tag’s contents if the condition is true, or any else or elsif sections if the condition isn’t true.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/inversion/template/iftag.rb', line 53 def render( renderstate ) evaluated_state = self.evaluate( renderstate ) evaluated_state = ! evaluated_state if self.inverted # Start out with rendering enabled if the tag body evaluates trueishly if evaluated_state self.log.debug "Initial state was TRUE; enabling rendering" renderstate.enable_rendering else self.log.debug "Initial state was FALSE; disabling rendering" renderstate.disable_rendering end # Set the tag state to track whether or not rendering has been enabled during the # 'if' for an 'else' or 'elsif' tag. renderstate.with_tag_data( rendering_was_enabled: renderstate.rendering_enabled? ) do super end renderstate.enable_rendering return nil end |