Go Template Engine

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

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:

  1. {{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 templateengines will need to supply there own implementation.

append

Add a variable to an array, or create an array; in the given context.

  1. {{append . "moreScripts" "js/jquery-ui-1.7.2.custom.min.js"}}
  2. {{range .moreStyles}}
  3. <link rel="stylesheet" type="text/css" href="/public/{{.}}">
  4. {{end}}

checkbox

Assists in constructing a HTML checkbox input element, eg:

  1. {{with $checkboxField := field "testField" .}}
  2. {{checkbox $checkboxField "someValue"}}
  3. {{end}}

date, datetime, timeago

Format a date according to the application’s default date and datetime format.

The example below assumes dateArg := time.Now():

  1. {{date .MyDate}}
  2. {{datetime .MyDateTime}}

even

Perform $in % 2 == 0. This is a convenience function that assists with table row coloring.

  1. {{range $index, $element := .results}}
  2. <tr class="{{if even $index}}light-row{{else}}dark-row{{end}}">
  3. ...
  4. </tr>
  5. {{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:
  1. {{with $field := field "booking.CheckInDate" .}}
  2. <p class="{{$field.ErrorClass}}">
  3. <strong>Check In Date:</strong>
  4. <input type="text" size="10" name="{{$field.Name}}"
  5. class="datepicker" value="{{$field.Flash}}"> *
  6. <span class="error">{{$field.Error}}</span>
  7. </p>
  8. {{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”].

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

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

option

Assists in constructing HTML option elements, in conjunction with the fieldhelper, eg:

  1. {{with $field := field "booking.Beds" .}}
  2. <select name="{{$field.Name}}">
  3. {{option $field "1" "One king-size bed"}}
  4. {{option $field "2" "Two double beds"}}
  5. {{option $field "3" "Three beds"}}
  6. </select>
  7. {{end}}

pad

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

  1. {{pad "my string", 8}}

pluralize

A helper for correctly pluralizing words.

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

radio

Assists in constructing HTML radio input elements, in conjunction with the fieldhelper, eg:

  1. {{with $field := field "booking.Smoking" .}}
  2. {{radio $field "true"}} Smoking
  3. {{radio $field "false"}} Non smoking
  4. {{end}}

raw

Prints raw, unescaped, text.

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

set

Set a variable in the given context.

  1. {{set . "title" "Basic Chat room"}}
  2. <h1>{{.title}}</h1>

slug

Create a slug

  1. {{slug "SomeThing String"}}

url

Outputs the reverse route for a Controller.Method, eg:

  1. <a href="{{url "MyApp.ContactPage"}}">Contact</a>
  2. 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:

  1. funcinit(){revel.TemplateFuncs["my_eq"]=func(a,binterface{})bool{returna==0||a==b}}

Examples

The sample applications try to demonstrate effective use ofGo Templates. In particular, please take a look at Booking app templates:

For example, the header looks like this:

  1. <html>
  2. <head>
  3. <title>{{.title}}</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <link rel="stylesheet" type="text/css" media="screen" href="/public/css/main.css">
  6. <link rel="shortcut icon" type="image/png" href="/public/img/favicon.png">
  7. {{range .moreStyles}}
  8. <link rel="stylesheet" type="text/css" href="/public/{{.}}">
  9. {{end}}
  10. <script src="/public/js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
  11. <script src="/public/js/sessvars.js" type="text/javascript" charset="utf-8"></script>
  12. {{range .moreScripts}}
  13. <script src="/public/{{.}}" type="text/javascript" charset="utf-8"></script>
  14. {{end}}
  15. </head>

And templates that include it look like this:

  1. {{set . "title" "Hotels"}}
  2. {{append . "moreStyles" "ui-lightness/jquery-ui-1.7.2.custom.css"}}
  3. {{append . "moreScripts" "js/jquery-ui-1.7.2.custom.min.js"}}
  4. {{template "header.html" .}}
GoDoc Reference
GitHub Labels

原文: https://revel.github.io/manual/templates-go.html