Inversion::
Template class
Superclass | Object |
Included Modules | |
Extended With |
|
The main template class.
Inversion
templates are the primary objects you’ll be interacting with. Templates can be created from a string:
Inversion::Template.new( template_source )
or from a file:
Inversion::Template.load( 'path/to/template.tmpl' )
Template
Options
Inversion
supports the [Configurability](rubygems.org/gems/configurability) API, and registers itself with the ‘templates` key. This means you can either add a `templates` section to your Configurability config, or call ::configure
yourself with a config Hash (or something that quacks like one).
To set options on a per-template basis, you can pass an options hash to either Inversion::Template::load
or Inversion::Template::new
, or set them from within the template itself using the [config tag](config). at Tags
The available options are:
Setting to false causes unknown tags used in templates to raise an Inversion::ParseError
. Defaults to ‘true`.
Dictates the behavior of exceptions during rendering. Defaults to ‘:comment`.
Exceptions are silently ignored.
Exceptions are rendered inline as comments.
Exceptions bubble up to the caller of Inversion::Template#render
.
Insert various Inversion
parse and render statements while rendering. Defaults to ‘false`.
When rendering debugging comments, the comment is started with these characters. Defaults to "<!--"
.
When rendering debugging comments, the comment is finished with these characters. Defaults to "-->"
.
An array of filesystem paths to search for templates within, when loaded or included with a relative path. The current working directory is always the last checked member of this. Defaults to []
.
The escaping used by tags such as ‘escape` and `pp`. Default: `:html`.
If a tag’s presence introduces a blank line into the output, this option removes it. Defaults to ‘true`.
Templates know when they’ve been altered on disk, and can dynamically reload themselves in long running applications. Setting this option creates a purposeful delay between reloads for busy servers. Defaults to ‘0` (disabled).
Disable getting/setting attributes that aren’t explicitly declared with a tag. Trying to get/set an attribute that isn’t declared in the template with this option enabled will result in a NoMethodError being raised.
Add the mixin to Template
Constants
- DEFAULT_CONFIG
Default config values
- VALID_ERROR_ACTIONS
Valid actions for ‘on_render_error’
Attributes
- config RW
- template_paths RW
- attributes R
The Hash of template attributes
- fragments R
The Hash of rendered template fragments
- node_tree R
The node tree parsed from the template source
- options R
The Template’s configuration options hash
- source R
The raw template source from which the object was parsed.
- source_file RW
The Pathname of the file the source was read from
Public Class Methods
Add one or more extension ‘modules` to Inversion::Template
. This allows tags to decorate the template class with new functionality.
Each one of the given ‘modules` will be included as a mixin, and if it also contains a constant called ClassMethods and/or PrependedMethods, it will also be extended/prepended (respectively) with it.
Example
Add a layout attribute to templates from a ‘layout’ tag:
class Inversion::Template::LayoutTag < Inversion::Tag module TemplateExtension def layout return @layout || 'default.tmpl' end module PrependedMethods def initialize( * ) super @layout = nil end end Inversion::Template.add_extensions( TemplateExtension ) # ... more tag stuff end
Configure the templating system.
Read a template object from the specified ‘path`.
Create a new Inversion:Template with the given ‘source`.
Public Instance Methods
Returns ‘true` if the template was loaded from a file and the file’s mtime is after the time the template was created.
Copy constructor – make copies of some internal data structures, too.
Return a human-readable representation of the template object suitable for debugging.
If the template was loaded from a file, reload and reparse it from the same file.
Render the template, optionally passing a render state (if, for example, the template is being rendered inside another template).
Protected Instance Methods
Add attributes for the given ‘node`’s identifiers.
Search for identifiers in the template’s node tree and declare an accessor for each one that’s found.
Install reader and writer methods for the attribute associated with the specified ‘key`.
Make method bodies
Proxy method: handle attribute readers/writers for attributes that aren’t yet defined.
Parse the given ‘source` into the template node tree.
Walk the template’s node tree, yielding each node in turn to the given block.