Inversion 'default' tag.
The default tag sets the default value of an attribute to a constant, the value of another attribute, or the results of evaluating a methodchain on an attribute.
<!-- Set a default width that can be overridden by the controller --> <?default width to 120 ?> <?default employees to [] ?> <!-- Default an attribute to the value of a second attribute --> <?default content to body ?> <!-- Set the title to the employee's name if it hasn't been set explicitly --> <?default title to "%s, %s" % [ employee.lastname, employee.firstname ] ?>
Create a new DefaultTag with the given name, which should be a valid Ruby identifier.
# File lib/inversion/template/defaulttag.rb, line 58 def initialize( body, linenum=nil, colnum=nil ) @name = nil @methodchain = nil @literal = nil @format = nil super # Add an identifier for the tag name self.identifiers << self.name.untaint.to_sym end
Render the tag as the body of a comment, suitable for template debugging.
# File lib/inversion/template/defaulttag.rb, line 90 def as_comment_body comment = "%s '%s': { " % [ self.tagname, self.name ] if self.methodchain comment << "template.%s%s" % [ self.identifiers.first, self.methodchain ] else comment << self.literal end comment << " }" comment << " with format: %p" % [ self.format ] if self.format return comment end
Set the specified value (if it's nil) before rendering.
# File lib/inversion/template/defaulttag.rb, line 105 def before_rendering( renderstate ) if val = renderstate.attributes[ self.name.to_sym ] self.log.info "Not defaulting %s: already set to %p" % [ self.name, val ] return nil end default = nil if chain = self.methodchain self.log.debug "Using methodchain %p to set default for %p" % [ chain, self.name ] default = renderstate.eval( 'self' + '.' + self.identifiers.first + chain ) else self.log.debug "Using literal %p to set default for %p" % [ self.literal, self.name ] default = renderstate.eval( self.literal ) default = self.format % default if self.format end self.log.debug " default value: %p" % [ default ] renderstate.attributes[ self.name.to_sym ] = default end
Generated with the Darkfish Rdoc Generator 2.