zoukankan      html  css  js  c++  java
  • {{.title}}

    revel使用Go官方的模板库。它会在两个目录查找模板文件: 1、应用的`views`目录以及它的所有子目录 2、revel库自己的`templates`目录 revel为错误页面提供了模板(在`dev`开发模式下,编译错误会更友好的显示),但是,如果用相同的名字创建一个模板,revel则会优先使用自己开发的模板。如 `app/views/errors/500.html` ##渲染内容 revel使用`RenderArgs`数据map来执行模板。除了从应用提供数据,revel也提供了: 1、“errors”, 从`Validation.ErrorMap`返回的错误信息 2、“flash”, 上一个请求携带的flash信息 ##模板函数 Go本身为模板提供了一些函数方便我们使用。revel也添加了一些实用的函数: **eq** 类似`a == b`的条件判断:
    **set** 在当前作用域设置一个变量: {{set . "title" "Basic Chat room"}}

    {{.title}}

    **append** 将一个给定的值添加至数组/切片,如果数组/切片不存在,则新建一个。 {{append . "moreScripts" "js/jquery-ui-1.7.2.custom.min.js"}} {{range .moreStyles}} {{end}} **field** 对Input表单字段进行操作。 给定一个字段名称,它将返回一个包含下列成员的struct: `Id`: 字段名,会转换为恰当的`HTML id`元素 `Name`: 字段名 `Value`: `RenderArgs`中当前字段的值 `Flash`: 当前字段的flash信息 `Error`: 任何一个与当前字段有关的错误信息 `ErrorClass`: `hasError`的原始信息,如果不存在则为空字符串 {{with $field := field "booking.CheckInDate" .}}

    Check In Date: * {{$field.Error}}

    {{end}} **option** 结合`field`模板函数生成`HTML option`元素 {{with $field := field "booking.Beds" .}} {{end}} **radio** 结合`field`模板函数生成`HTML radio`元素 {{with $field := field "booking.Smoking" .}} {{radio $field "true"}} Smoking {{radio $field "false"}} Non smoking {{end}} **nl2br** 生成HTML的换行标签 You said:
    {{nl2br .commentText}}
    **pluralize** 转换单词的复数形式 There are {{.numComments}} comment{{pluralize (len comments) "" "s"}} **Including** Go允许将一个模板嵌入至另一个模板 {{template "header.html" .}} 有两点需要注意: 1、路径必须是`app/views`的相对路径 2、任何被嵌入的模板必须位于`app/views`目录的根位置(这个限制希望是暂时的) **Tips** revel的示例程序说明了如何有效的使用模板,请特别留意这两个示例: *revel/samples/booking/app/views/header.html* *revel/samples/booking/app/views/header.html* 它利用模板函数设置模板自己的html header和样式: {{.title}} {{range .moreStyles}} {{end}} {{range .moreScripts}} {{end}} 引用html header模板: {{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" .}} ##自定义模板函数 revel应用可以注册自定义模板函数: func init() { revel.TemplateFuncs["eq"] = func(a, b interface{}) bool { return a == b } }
  • 相关阅读:
    rabbitmq系统学习(三)集群架构
    rabbitmq系统学习(二)
    rabbitmq系统学习(一)
    itext实现pdf自动定位合同签订
    itext7知识点研究(PDF编辑)
    itext实现合同尾部签章部分自动添加,定位签名
    ELK实战(Springboot日志输出查找)
    [Wireshark]_002_玩转数据包
    [Wireshark]_001_入门
    [Objective-C] 014_Objective-C 代码规范指南
  • 原文地址:https://www.cnblogs.com/hangxin1940/p/3269211.html
Copyright © 2011-2022 走看看