Revel模板引擎Template基本语法
- 注釋
- 變量渲染
- 定義局部變量
- 定義子模板
- 管道函數(shù)
用法1:
//調(diào)用一個無參的函數(shù),類似function() {{FuncName}}用法2:
//調(diào)用一個有參的函數(shù),類似split(str,",") {{split .str "," }}用法3:
將豎線“|”左邊的變量值作為函數(shù)參數(shù)傳送
- 條件判斷
【技巧】如果需要去除空行,則需要使用以下方法
{{- if condition}} //... {{- else}} //... {{- end}}- 遍歷
用法一:
{{range $key,$value := .Lists }}//{{$key}} => {{$value}} {{end}}用法二:
{{range .Lists }}//{{.}} {{end}}用法三:
{{range .var}}//{{.}} {{else}}//沒有可遍歷的值時,則執(zhí)行else {{end}}- 嵌入子模板
預定義的模板全局函數(shù)
- and {{and x y}} //等同于Golang中的:x and y
- or
-
call
//call 第一個參數(shù)必須是一個函數(shù),其余參數(shù)作為該函數(shù)的參數(shù) {{call add 1 2}} -
html
轉(zhuǎn)義文本中的html標簽,如將“<”轉(zhuǎn)義為“<”,“>”轉(zhuǎn)義為“>”等 -
index
{{index .array 1 2 3}} //表示:array[1][2][3] //array必須是一個map、slice或數(shù)組
返回index后面的第一個參數(shù)的某個索引對應的元素值,其余的參數(shù)為索引值 -
js
返回用JavaScript的escape(編碼)處理后的文本 -
len
返回參數(shù)的長度值(int類型) -
not
返回參數(shù)的否定值(bool類型) -
print
fmt.Sprint的別名 -
printf
fmt.Sprintf的別名 -
println
fmt.Sprintln的別名 -
urlquery
url參數(shù)編碼
Revel函數(shù)
-
append
{{append . "moreScripts" "js/jquery-ui-1.7.2.custom.min.js"}}{{range .moreStyles}}<link rel="stylesheet" type="text/css" href="/public/{{.}}"> {{end}}
向數(shù)組添加變量或創(chuàng)建數(shù)組 -
checkbox
{{with $checkboxField := field "testField" .}}{{checkbox $checkboxField "someValue"}} {{end}}
幫助構造HTML復選框輸入元素,例如: -
date, datetime, timeago
{{date .MyDate}} {{datetime .MyDateTime}}
根據(jù)應用程序設置的默認日期和時間格式格式化日期。 -
even 取余,N % 2 == 0 這是一個方便表格行著色的函數(shù)。
{{range $index, $element := .results}} <tr class="{{if even $index}}light-row{{else}}dark-row{{end}}">//... </tr> {{end}} -
field
{{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}}
輸入字段的助手函數(shù)
給定一個字段名,它返回一個包含以下成員的結構:
Id: 字段名,已轉(zhuǎn)換為適合作為HTML元素ID。
Name: 字段名
Value: 當前ViewArgs字段的值
Options:當前ViewArgs選項列表
Flash: 字段的flash值.
Error: 有與此字段關聯(lián)的錯誤信息
ErrorClass: 原始字符串“haserror”, 如果有錯誤時顯示此樣式, 否則為 ""選項列表可以使用閃存來設置選項
c.ViewArgs["options"] = map[string][]string{"record.Status": map[string][]string{"Started","Ongoing", "Finished"}, } -
i18ntemplate
-
msg
-
nl2br
<div class="comment">{{nl2br .commentText}}</div>
將換行符轉(zhuǎn)換為HTML換行符。 -
option
{{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}}
構造HTML option 標簽的助手函數(shù) -
pad 在給定的字符串上加上空格字符"?"
{{pad "my string", 8}} -
pluralize 正確復數(shù)單詞的助手函數(shù)。
There are {{.numComments}} comment{{pluralize (len comments) "" "s"}} -
radio 構造HTML Radio元素,例如:
{{with $field := field "booking.Smoking" .}}{{radio $field "true"}} Smoking{{radio $field "false"}} Non smoking {{end}} -
raw
<div class="body">{{raw .blogBody}}</div>
打印未編碼的原始文本。 -
set 設置變量內(nèi)容
{{set . "title" "Basic Chat room"}} <h1>{{.title}}</h1> -
SLUG 創(chuàng)建一個slug
-
url
<a href="{{url "MyApp.ContactPage"}}">Contact</a> Click <a href="{{url "Products.ShowProduct" 123}}">here</a> for more.
根據(jù)Controller.Method解析路由地址 -
自定義函數(shù)
布爾函數(shù)
- 【eq】 返回表達式“arg1 == arg2”的布爾值
- 【ne】 返回表達式“arg1 != arg2”的布爾值
- 【lt】 返回表達式“arg1 < arg2”的布爾值
- 【le】 返回表達式“arg1 <= arg2”的布爾值
- 【gt】 返回表達式“arg1 > arg2”的布爾值
- 【ge】 返回表達式“arg1 >= arg2”的布爾值
總結
以上是生活随笔為你收集整理的Revel模板引擎Template基本语法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 传感器是新技术革命和当前信息社会的重要技
- 下一篇: Angular 简介