May 15, 2021 Revel
Revel uses the Go template to find the template in two directories:
views
application (including all subdirectdirectles)
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
.
Revel renders templates using RenderArgs map. In addition to the data transmitted by the developer, Revel also provides some useful data:
Validation.ErrorMap
Go provides some template functions. R evel has also added some template functions. Read the documentation below or view the source code .
A simple "a - b" test.
For example:
<div class="message {{if eq .User "you"}}you{{end}}">
Set a variable in the context of the current template
For example:
{{set . "title" "Basic Chat room"}}
<h1>{{.title}}</h1>
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}}
Input field auxiliary function.
Given a field name, the function generates a structure that contains the following members:
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}}
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}}
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}}
Break that converts line breaks into HTML.
For example:
You said:
<div class="comment">{{nl2br .commentText}}</div>
An auxiliary plulus function
For example:
There are {{.numComments}} comment{{pluralize (len comments) "" "s"}}
Output native, un escaped text
For example:
<div class="body">{{raw .blogBody}}</div>
Go templates allow you to include other templates in the template, such as:
{{template "header.html" .}}
Note: The relative path is
app/views
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" .}}
Applications can register custom template functions
For example:
func init() {
revel.TemplateFuncs["eq"] = func(a, b interface{}) bool { return a == b }
}