Revel currently uses Go’s built in html/template package (coming soon is different templating engines)

NOTE@notzippy: 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

Directories and scanned for templates in the following order:

  1. The application app/views/ directory and subdirectories.
  2. revel core templates/ directory.
  3. Otherwise a 500 error as template not found (but in dev mode shows debug info)

For example, given a controller/action Hello.World(), Revel will:

  • look for a template file named views/Hello/World.html.
  • and if not found, show views/errors/500.html
  • and if that’s not found, use Revel’s built-in templates/errors/500.html

Template file names are case insensitive so the following will be treated as the same:

  • views/hello/world.html
  • views/HeLlO/wOrLd.HtMl

However, on **nix based file systems (and for example with index.html and IndeX.html), duplicate cased file names are to be avoided as its unpredictable which one will be considered.

Revel provides templates for error pages (see code) and these display the developer friendly compilation errors in dev mode. An application may override them by creating a template of the equivalent template name, e.g. app/views/errors/404.html.

Template Delimiters

Revel provides configurable Template Delimiters via app.conf.

Render Context

Revel executes the template using the ViewArgs data map[string]interface{}. Aside from application-provided data, Revel provides the following entries:

Including Other Templates

Go Templates allow you to compose templates by inclusion. For example:

{{template "header.html" .}}
Note: Paths are relative to app/views

Template Functions

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

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

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 &nbsp; 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.Action, 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" .}}
GoDoc Reference
GitHub Labels