Go Template Engine

The Go template engine is the default one used by Revel. It requires no application settings.

The Go template engine parser strips html comments from the content (see here). The reasoning behind this is explained here. Use a custom function to protect your inline comment to resolve this

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" .}}
Note: Paths are relative to 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.


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/{{.}}">


Assists in constructing a HTML checkbox input element, eg:

{{with $checkboxField := field "testField" .}}
    {{checkbox $checkboxField "someValue"}}

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}}


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}}">


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 "".


{{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>

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"},


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



Convert newlines to HTML breaks.

You said:
<div class="comment">{{nl2br .commentText}}</div>


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"}}


Pads the given string with &nbsp; to the given width, eg:.

{{pad "my string", 8}}


A helper for correctly pluralizing words.

There are {{.numComments}} comment{{pluralize (len comments) "" "s"}}


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


Prints raw, unescaped, text.

<div class="body">{{raw .blogBody}}</div>


Set a variable in the given context.

{{set . "title" "Basic Chat room"}}



Create a slug

{{slug "SomeThing String"}}


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


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:

    <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/{{.}}">
    <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>

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" .}}
GoDoc Reference
GitHub Labels