Go Template Engine
The Go template engine is the default one used by Revel. It requires no application settings.
Template Delimiters
Revel provides configurable Template Delimiters via app.conf
.
Including Other Templates
Go Templates allow you to compose templates by inclusion. For example:
app/views
Go Template Functions
These built-in functions are only applicable to the Go template engine, other template engines will need to supply there own implementation.
- Go provides a few native template functions.
- Revel adds to those. Read the documentation below or check out the source code.
append
Add a variable to an array, or create an array; in the given context.
checkbox
Assists in constructing a HTML checkbox input
element, eg:
date, datetime, timeago
Format a date according to the application’s default date and datetime format.
The example below assumes dateArg := time.Now()
:
even
Perform $in % 2 == 0
. This is a convenience function that assists with table row coloring.
field
A helper for input fields godoc.
Given a field name, it returns a struct containing the following members:
- Id: the field name, converted to be suitable as a HTML element ID.
- Name: the field name
- Value: the value of the field in the current
ViewArgs
- Options: the list of options in the current
ViewArgs
- Flash: the flash value of the field.
- Error: the error message, if any is associated with this field.
- ErrorClass: the raw string
"hasError"
, if there was an error, else""
.
Example:
Options Example :
in radio.html template
The option list can be set as the way set field value (which using flash). Options, use ViewArgs[“options”].
c.ViewArgs["options"] = map[string][]string{
"record.Status": map[string][]string{"Started","Ongoing", "Finished"},
}
i18ntemplate
Include another template through the revel template engine loader
Arguments:
- template (string) required The template name you want to render
- viewArgs (interface{}) optional The data to be piped to the
- region (string) optional The region, if not provided it will
attempt to extract the .currentRegion
from the viewArgs
Interesting tidbit, you can
msg
nl2br
Convert newlines to HTML breaks.
option
Assists in constructing HTML option
elements, in conjunction with the field
helper, eg:
pad
Pads the given string with
to the given width, eg:.
pluralize
A helper for correctly pluralizing words.
radio
Assists in constructing HTML radio input
elements, in conjunction with the field
helper, eg:
raw
Prints raw, unescaped, text.
set
Set a variable in the given context.
slug
Create a slug
url
Outputs the reverse route for a Controller.Method
, eg:
Custom Functions
Applications may register custom functions for use in templates.
Here is an example:
Examples
The sample applications try to demonstrate effective use of Go Templates. In particular, please take a look at Booking app templates:
It takes advantage of the helper functions to set the title and extra styles in the template itself.
For example, the header looks like this:
And templates that include it look like this: