Inversion import tag.
The import tag copies one or more attributes from an enclosing template so they only need to be set once.
<?import txn ?> <?import foo, bar ?>
the names of the attributes to import
Create a new ImportTag with the given
name
, which should be a valid Ruby identifier.
# File lib/inversion/template/importtag.rb, line 20
def initialize( body, linenum=nil, colnum=nil )
super
@attributes = body.split( /\s*,\s*/ ).collect {|name| name.untaint.strip.to_sym }
end
Render the tag as the body of a comment, suitable for template debugging.
# File lib/inversion/template/importtag.rb, line 66
def as_comment_body
return "Import %s" % [ self.attributes.join(', ') ]
end
Merge the inherited renderstate into the current template's
renderstate
.
# File lib/inversion/template/importtag.rb, line 35
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