AttrTag

class
Superclass
Inversion::Template::CodeTag

Inversion attribute tag.

Attribute tags add an accessor to a template like 'attr_accessor' does for Ruby classes.

Syntax

<?attr foo ?>
<?attr "%0.2f" % foo ?>

Attributes

format[RW]

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

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 AttrTag with the given name, which should be a valid Ruby identifier. The linenum and colnum should be the line and column of the tag in the template source, if available.

# File lib/inversion/template/attrtag.rb, line 53
def initialize( body, linenum=nil, colnum=nil )
        @name        = nil
        @format      = nil
        @methodchain = 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/attrtag.rb, line 101
def as_comment_body
        comment = "%s: { template.%s" % [ self.tagname, self.name ]
        comment << self.methodchain if self.methodchain
        comment << " }"
        comment << " with format: %p" % [ self.format ] if self.format

        return comment
end
anchor
evaluate( renderstate )

Evaluate the body of the tag in the context of renderstate and return the results.

# File lib/inversion/template/attrtag.rb, line 94
def evaluate( renderstate )
        code = [ self.name.to_s, self.methodchain ].join( '' )
        return renderstate.eval( code )
end
anchor
render( renderstate )

Render the tag attributes of the specified renderstate and return them.

# File lib/inversion/template/attrtag.rb, line 81
def render( renderstate )
        value = self.evaluate( renderstate ) # :FIXME: or return value # nil or false?

        # Apply the format if there is one
        if self.format && value
                return self.format % value
        else
                return value
        end
end