DefaultTag

class
Superclass
Inversion::Template::CodeTag

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.

Syntax

<!-- 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 ] ?>

Attributes

format[RW]

the format string used to format the attribute in the template (if one was declared)

literal[RW]

The literal, if the tag had one (as opposed to an attribute or methodchain)

methodchain[RW]

the chain of methods that should be called (if any).

name[RW]

the name of the attribute

Public Class Methods

anchor
new( body, linenum=nil, colnum=nil )

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

Public Instance Methods

anchor
as_comment_body()

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
anchor
before_rendering( renderstate )

Set the specified value (if it's nil) before rendering.

# File lib/inversion/template/defaulttag.rb, line 105
def before_rendering( renderstate )
        existing_value = renderstate.attributes[ self.name.to_sym ]
        unless existing_value.nil?
                self.log.info "Not defaulting %s: already set to %p" %
                        [ self.name, existing_value ]
                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
anchor
render( renderstate )

Render as the empty string.

# File lib/inversion/template/defaulttag.rb, line 131
def render( renderstate )
        return ''
end