Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Revel template


May 15, 2021 Revel


Table of contents


Revel uses the Go template to find the template in two directories:

  • Views directory views application (including all subdirectdirectles)
  • Revel's templates directory.

For example, if you have Hello whose World Revel looks for a views/Hello/World.html named " . The template name is case insensescing, views/hello/world.html and views/HeLlO/wOrLd.HtMl matching templates.

Revel provides error page templates (friendly display compilation errors in development mode) that developers can also rewrite, such app/views/errors/500.html .

The rendering context

Revel renders templates using RenderArgs map. In addition to the data transmitted by the developer, Revel also provides some useful data:

  • "errors" - Verify errors (map, refer to the document Validation.ErrorMap
  • "flash" - data from the last request flash

Template functionality

Go provides some template functions. R evel has also added some template functions. Read the documentation below or view the source code .

eq

A simple "a - b" test.

For example:

<div class="message {{if eq .User "you"}}you{{end}}">

set

Set a variable in the context of the current template

For example:

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

<h1>{{.title}}</h1>

append

Add variables to an array, or create an array in the context of a template

For example:

{{append . "moreScripts" "js/jquery-ui-1.7.2.custom.min.js"}}

{{range .moreStyles}}
  <link rel="stylesheet" type="text/css" href="/public/{{.}}">
{{end}}

field

Input field auxiliary function.

Given a field name, the function generates a structure that contains the following members:

  • Id: Field Id, the ID converted to an HTML field.
  • Name: Field name
  • Value: The value of the field in the current context
  • Flash: The field value brought back
  • Error: Field error message (if there is an error)
  • ErrorClass: The original string "hasError", if there is no error is a "".

Browse godoc.

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

option

Use secondary functions to generate HTML option fields.

For example:

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

radio

Use secondary functions to generate HTML radio input fields

For example:

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

nl2br

Break that converts line breaks into HTML.

For example:

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

pluralize

An auxiliary plulus function

For example:

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

raw

Output native, un escaped text

For example:

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

Including

Go templates allow you to include other templates in the template, such as:

{{template "header.html" .}}

Note: The relative path is app/views

Warm tips

Revel's application uses the Go template efficiently, so take a look at the following example:

  • revel/samples/booking/app/views/header.html
  • revel/samples/booking/app/views/Hotels/Book.html

Use secondary functions to set the title and additional styles for the template.

For example:

<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="/attachments/image/cimg/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="/attachments/image/cimg/script>
    {{range .moreScripts}}
      <script src="/attachments/image/cimg/{{.}}" type="text/javascript" charset="utf-8"></script>
    {{end}}
  </head>

Use this in a template:

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

Custom template functions

Applications can register custom template functions

For example:

func init() {
    revel.TemplateFuncs["eq"] = func(a, b interface{}) bool { return a == b }
}