Revel allows for building and using custom template engines; the process for building your own engine is as follows
Building a new Template Engine
A template engine must implement the TemplateEngine
interface. To register a new engine in Revel :
- Define it as a module to be loaded in the
app.conf. - In the
init()function of the engine, register the engine in revel by calling:
RegisterTemplateLoader(key string, loader func(loader *TemplateLoader) (TemplateEngine, error)) (err error) - Specify the template engines to be used by setting the
template.enginesconfiguration option to the names of the engine to be used (a comma delimited list).
Template Engine Interface
-
Event(event int, arg interface{}) Called when a
revel.TemplateRefreshorrevel.TemplateRefreshCompleteoccurs. This allows the engine to do a pre-initialize, before the templates are loaded, and could also do some form of memory release once the templates are loaded. -
Handles(templateView *TemplateView) bool Called for every view found, revel creates a
Trevel.TemplateViewinstance and calls every engine to see if they are the one to be used to parse the view -
ParseAndAdd(basePath *TemplateView) error Called on application startup, or when templates have been changed.
-
Name() string Called to fetch the name of the template engine.
-
Lookup(templateName string) revel.Template Called to fetch the template, see below for what the template interface implements.
There is a helper struct called revel.EngineHandles which can be used to examine the view to check to see if it contains a shebang or a file extenstion that matches the name of the engine , see revel.GoEngine for some examples
Template Interface
Name() stringName of templateContent() []stringThe content of the template as a string (Used in error handling).Render(wr io.Writer, context interface{}) errorCalled by the server to render the template out the io.Writer, context contains the view args to be passed to the template.Location() string// The full path to the file on the disk.
There is a helper struct called revel.TemplateView, which implements the Location function,
see
revel.GoEngine for some examples