module Roda::RodaPlugins::Inversion
A templated content-generation plugin for Roda
Apps. It uses the Inversion templating system.
It adds:
-
a preloaded/cached template table
-
a mechanism for fetching templates from the table
-
a global layout template which is automatically wrapped around responses
Note: This is based almost entirely off the Strelka plugin of identical function.
Usage¶ ↑
To use it, just load the :inversion
plugin in your app:
plugin :inversion, '/path/to/templates/directory'
You can specify more than one templates directory; they’ll be searched in the order you specify for the named template and the first match wins:
plugin :inversion '/more/specific/templates', '/generic/app/templates'
Then declare one or more templates that your application will use:
templates console: 'views/console.tmpl', proctable: 'partials/proctable.tmpl'
Then, inside your app, you can fetch a copy of one or more of the templates and return it as the response:
r.root do tmpl = template( :console ) tmpl.message = "Everything's up." tmpl.proctable = proctable return tmpl end
You can also just return the template if you don’t need to do anything else to the response.
When returning a template, either in the body of the response or directly, it will automatically set a few attributes for commonly-used objects:
- request
-
The current Roda::Request
- response
-
The current Roda::Response
- app
-
The application object (Roda::App instance).
If your app will only be loading and returning a template without doing anything with it, you can return just its name:
r.root do return :console end
It will be loaded, set as the response body, and the above common objects added to it.
Layouts¶ ↑
Very often, you’ll want all or most of the views in your app to share a common page layout. To accomplish this, you can declare a layout template:
layout 'layout.tmpl'
Any template that you return will be set as the ‘body’ attribute of this layout template (which you’d place into the layout with <?attr body ?>
) and the layout rendered as the body of the response.
Note that if you want any of the “common objects” from above with a layout template, they’ll be set on it since it’s the top-level template, but you can still access them using the <?import ?>
directive:
<?import request, app ?>
Template Locations¶ ↑
Inversion
looks for templates in a load path much like Ruby does for libraries that you ‘require’. It contains just the current working directory by default. You can add your own template directories via the config file (under template_paths
in the templates
section), or programmatically from your application.
Public Class Methods
Add the specified template_paths
to Inversion
.
# File lib/roda/plugins/inversion.rb, line 96 def self::configure( app, *template_paths ) ::Inversion::Template.template_paths.concat( template_paths ) end