Class: Inversion::Template::ImportTag
- Defined in:
- lib/inversion/template/importtag.rb
Overview
Inversion import tag.
The import tag copies one or more attributes from an enclosing template so they only need to be set once.
Syntax
<?import txn ?>
<?import foo, bar ?>
Constant Summary
Constants inherited from Tag
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
the names of the attributes to import.
Attributes inherited from Tag
Attributes inherited from Node
Instance Method Summary collapse
-
#as_comment_body ⇒ Object
Render the tag as the body of a comment, suitable for template debugging.
-
#initialize(body, linenum = nil, colnum = nil) ⇒ ImportTag
constructor
Create a new ImportTag with the given
name, which should be a valid Ruby identifier. -
#render(renderstate) ⇒ Object
Merge the inherited renderstate into the current template’s
renderstate.
Methods inherited from Tag
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
Methods included from AbstractClass::ClassMethods
Methods inherited from Node
#after_appending, #after_rendering, #before_appending, #before_rendering, #is_container?, #location
Constructor Details
#initialize(body, linenum = nil, colnum = nil) ⇒ ImportTag
Create a new ImportTag with the given name, which should be a valid Ruby identifier.
22 23 24 25 |
# File 'lib/inversion/template/importtag.rb', line 22 def initialize( body, linenum=nil, colnum=nil ) super @attributes = body.split( /\s*,\s*/ ).collect {|name| name.strip.to_sym } end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
the names of the attributes to import
33 34 35 |
# File 'lib/inversion/template/importtag.rb', line 33 def attributes @attributes end |
Instance Method Details
#as_comment_body ⇒ Object
Render the tag as the body of a comment, suitable for template debugging.
68 69 70 |
# File 'lib/inversion/template/importtag.rb', line 68 def as_comment_body return "Import %s" % [ self.attributes.join(', ') ] end |
#render(renderstate) ⇒ Object
Merge the inherited renderstate into the current template’s renderstate.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/inversion/template/importtag.rb', line 37 def render( renderstate ) if (( cstate = renderstate.containerstate )) # self.log.debug "Importing inherited attributes: %p from %p" % # [ @attributes, cstate.attributes ] # Pick out the attributes that are being imported inherited_attrs = @attributes.inject( {} ) do |attrs, key| attrs[ key ] = cstate.attributes[ key ] attrs end # Merge, but overwrite unset values with inherited ones renderstate.attributes.merge!( inherited_attrs ) do |key, oldval, newval| if oldval.nil? # self.log.debug "Importing attribute %p: %p" % [ key, newval ] newval else # self.log.debug "Not importing attribute %p: already set to %p" % [ key, oldval ] oldval end end else self.log.debug "No-op import: no parent attributes set." end return nil end |