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:
{{template "header.html" .}}
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.
{{append . "moreScripts" "js/jquery-ui-1.7.2.custom.min.js"}}
{{range .moreStyles}}
<link rel="stylesheet" type="text/css" href="/public/{{.}}">
{{end}}
checkbox
Assists in constructing a HTML checkbox input
element, eg:
{{with $checkboxField := field "testField" .}}
{{checkbox $checkboxField "someValue"}}
{{end}}
date, datetime, timeago
Format a date according to the application’s default date and datetime format.
The example below assumes dateArg := time.Now()
:
{{date .MyDate}}
{{datetime .MyDateTime}}
even
Perform $in % 2 == 0
. This is a convenience function that assists with table row coloring.
{{range $index, $element := .results}}
<tr class="{{if even $index}}light-row{{else}}dark-row{{end}}">
...
</tr>
{{end}}
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:
{{with $field := field "booking.CheckInDate" .}}
<p class="{{$field.ErrorClass}}">
<strong>Check In Date:</strong>
<input type="text" size="10" name="{{$field.Name}}"
class="datepicker" value="{{$field.Flash}}"> *
<span class="error">{{$field.Error}}</span>
</p>
{{end}}
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.
You said:
<div class="comment">{{nl2br .commentText}}</div>
option
Assists in constructing HTML option
elements, in conjunction with the field
helper, eg:
{{with $field := field "booking.Beds" .}}
<select name="{{$field.Name}}">
{{option $field "1" "One king-size bed"}}
{{option $field "2" "Two double beds"}}
{{option $field "3" "Three beds"}}
</select>
{{end}}
pad
Pads the given string with
to the given width, eg:.
{{pad "my string", 8}}
pluralize
A helper for correctly pluralizing words.
There are {{.numComments}} comment{{pluralize (len comments) "" "s"}}
radio
Assists in constructing HTML radio input
elements, in conjunction with the field
helper, eg:
{{with $field := field "booking.Smoking" .}}
{{radio $field "true"}} Smoking
{{radio $field "false"}} Non smoking
{{end}}
raw
Prints raw, unescaped, text.
<div class="body">{{raw .blogBody}}</div>
set
Set a variable in the given context.
{{set . "title" "Basic Chat room"}}
<h1>{{.title}}</h1>
slug
Create a slug
{{slug "SomeThing String"}}
url
Outputs the reverse route for a Controller.Method
, eg:
<a href="{{url "MyApp.ContactPage"}}">Contact</a>
Click <a href="{{url "Products.ShowProduct" 123}}">here</a> for more.
Custom Functions
Applications may register custom functions for use in templates.
Here is an example:
func init() {
revel.TemplateFuncs["my_eq"] = func(a, b interface{}) bool {
return a == 0 || a == b
}
}
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:
<html>
<head>
<title>{{.title}}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" media="screen" href="/public/css/main.css">
<link rel="shortcut icon" type="image/png" href="/public/img/favicon.png">
{{range .moreStyles}}
<link rel="stylesheet" type="text/css" href="/public/{{.}}">
{{end}}
<script src="/public/js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="/public/js/sessvars.js" type="text/javascript" charset="utf-8"></script>
{{range .moreScripts}}
<script src="/public/{{.}}" type="text/javascript" charset="utf-8"></script>
{{end}}
</head>
And templates that include it look like this:
{{set . "title" "Hotels"}}
{{append . "moreStyles" "ui-lightness/jquery-ui-1.7.2.custom.css"}}
{{append . "moreScripts" "js/jquery-ui-1.7.2.custom.min.js"}}
{{template "header.html" .}}