通过以下文章,掌握了 Go 模板引擎 的基本用法:

但在开始学习 Beego 框架的 模板嵌套 模块源码时,有点似懂非懂的感觉。认真研究了一段时间,总算搞懂了 其本质的原理:

1、Beego 底层用的是 Go 自带的模板引擎,因此,只需要继续研究 Go 自带的 模板嵌套 的使用方法即可;

2、主要涉及到 4 个模板方法:New()、Parse()、Lookup()、Execute(),先看几个例子:

示例1:

tpl, _ := template.New("mod1").Parse(`我是mod1`)
tpl, _ = tpl.New("mod2").Parse(`我是mod2`)
tpl, _ = tpl.New("mod3").Parse(`我是mod3`) tpl.Execute(os.Stdout, nil)
fmt.Println("\n++++++++++++++++")
fmt.Println(tpl.Name())

输出:

我是mod3
++++++++++++++++
mod3

示例2:

tpl, _ := template.New("mod1").Parse(`我是mod1`)
tpl, _ = tpl.New("mod2").Parse(`我是mod2`)
tpl, _ = tpl.New("mod3").Parse(`我是mod3`) tpl2 := tpl.Lookup("mod2") if tpl2 != nil {
tpl2.Execute(os.Stdout, nil)
}

输出:

我是mod2

示例3:

tpl, _ := template.New("mod1").Parse(`我是mod1`)
tpl, _ = tpl.New("mod2").Parse(`{{define "Title"}}我是mod2_标题{{end}}我是mod2_内容`)
tpl, _ = tpl.New("mod3").Parse(`我是mod3`) tpl2 := tpl.Lookup("mod2") if tpl2 != nil {
tpl2.Execute(os.Stdout, nil)
}

输出:

我是mod2_内容

示例4:

tpl, _ := template.New("mod1").Parse(`我是mod1`)
tpl, _ = tpl.New("mod2").Parse(`{{define "Title"}}我是mod2_标题{{end}}我是mod2_内容`)
tpl, _ = tpl.New("mod3").Parse(`我是mod3`) tpl2 := tpl.Lookup("Title") if tpl2 != nil {
tpl2.Execute(os.Stdout, nil)
}

输出:

我是mod2_标题

示例5:

tpl, _ := template.New("mod1").Parse(`我是mod1`)
tpl, _ = tpl.New("mod2").Parse(`{{define "Title"}}我是mod2_标题{{end}}我是mod2_内容`)
tpl, _ = tpl.New("mod3").Parse(`我是mod3`)
tpl = tpl.New("mod4")
tpl, _ = tpl.New("mod5").Parse("我是mod5") fmt.Println(tpl.Name())
fmt.Println("+++++++++++++++") tpl2 := tpl.Lookup("mod4")
if tpl2 != nil {
tpl2.Execute(os.Stdout, nil)
} tpl3 := tpl.Lookup("mod5")
if tpl3 != nil {
tpl3.Execute(os.Stdout, nil)
}

输出:

mod5
+++++++++++++++
我是mod5

示例6:

tpl, _ := template.New("mod1").Parse(`我是mod1`)
tpl, _ = tpl.New("mod2").Parse(`{{define "Title"}}我是mod2_标题{{end}}我是mod2_内容`) tpl, _ = tpl.New("mod1").Parse(`我是mod1_内容{{define "Title"}}我是mod1_标题{{end}}`) fmt.Println(tpl.Name())
fmt.Println("+++++++++++++++") tpl2 := tpl.Lookup("mod1")
if tpl2 != nil {
tpl2.Execute(os.Stdout, nil)
} fmt.Println("\n+++++++++++++++") tpl3 := tpl.Lookup("Title")
if tpl3 != nil {
tpl3.Execute(os.Stdout, nil)
}

输出:

mod1
+++++++++++++++
我是mod1_内容
+++++++++++++++
我是mod1_标题

示例7:

tpl, _ := template.New("mod1").Parse(`我是mod1`)
tpl, _ = tpl.New("mod2").Parse(`{{define "Title"}}我是mod2_标题{{end}}我是mod2_内容`)
tpl, _ = tpl.New("mod3").Parse(`我是mod3_{{template "mod1"}}_{{template "Title"}}`) tpl2 := tpl.Lookup("mod3") if tpl2 != nil {
tpl2.Execute(os.Stdout, nil)
}

输出:

我是mod3_我是mod1_我是mod2_标题

总结:

1、一个模板对象,可以有多个“名称”,但是通过 .Name() 方法,只返回最后一次通过 .New() 方法新建的“名称”。特别注意:此时的 模板对象 的 数据段也是指向该“名称”所绑定的“字符串”数据;

2、一个模板对象,可以有多个“名称”,但是他们是相互独立的,每个“名称”,通过 .Parse() 方法,可以与一段“字符串”数据对象建立关联。也就是说,后期通过 .Lookup("某个名称") 方法获取到的(模板)对象,其当前数据段,指向该“字符串”,再通过 .Execute() 方法输出的内容,也是跟该“字符串”相关的内容;

3、在某个“名称”的数据“字符串”对象中,可以用 define 定义子模块名,如 {{define "子模块名"}}子模块内容{{end}},该 “子模块”又独立于 当前“名称”,即 当前“名称”的输出内容,不包含 “子模板”的内容,参考 示例3

只输出“我是mod2_内容”,并没有输出“我是mod2_标题”

4、特别注意,通过 {{define "子模块名"}}子模块内容{{end}} 定义的名称,并不是 模板对象 的一个“名称”,但是我们同样可以用  .Lookup("某个名称") 方法获取到对象,其当前数据段,指向该 “子模块”,再通过 .Execute() 方法输出的内容,也是跟该“子模块”相关的内容,查看 示例4

只输出“我是mod2_标题”

5、可用重复定义“名称”和 “子模块”,但后面定义的会覆盖前面定义的,即 只有最后一次定义的有效。参考 示例6

6、模板内容(“名称”的“字符串”对象)中,可通过 template 关键字来 引用/包含 “名称”和 “子模块”,如 {{template "名称/子模块"}},参考 示例7

搞明白了这些基础知识,再回过头看 Beego 的模板模块,已是非常简单了!

[Go] Beego 模板嵌套 使用总结的更多相关文章

  1. beego——模板语法

    一.基本语法 go统一使用{{和}}作为左右标签,没有其它的标签符号. 使用"."来访问当前位置的上下文,使用"$"来引用当前模板根级的上下文,使用$var来访 ...

  2. Beego模板 循环和判断几个例子

    Beego模板 循环和判断几个例子 Beego的前端几乎是另一种语言.一些循环.判断,不细看文档真的做不出来. 0. Beego的View模板语法规则: beego前端(view)统一使用了 {{ 和 ...

  3. beego——模板函数

    beego 支持用户定义模板函数,但是必须在 beego.Run() 调用之前,设置如下: func hello(in string)(out string){ out = in + "wo ...

  4. beego——模板处理

    beego的模板处理引擎采用的是Go内置的html/template包进行处理,而且beego的模板处理逻辑是采用了缓存编译方式, 也就是所有的模板会在beego应用启动的时候全部编译然后缓存在map ...

  5. (55)zabbix模板嵌套

    在zabbix使用过程中,在某些情况下,一个host需要link多个模板.这么做显得比较麻烦,很容易忘记到底要link哪些模板,我想link一个模板就达成这个目标,行不行?然没问题,zabbix模板内 ...

  6. django - Templates模板嵌套语法

    模板继承 1.继承母板:{% extends '母板html文件名称' %} 2.包含子模板:{% include  '子母板html 文件名' %} 模板内容分块 {% block <分块名& ...

  7. Go 收藏

    Golang 定位解决分布式系统,服务器应用开发,主要竞争对手是 Java.Python 之类:Rust 定位解决单机安全问题,高性能场景偏系统底层开发,主要竞争对手就是 C 和 C++. Golan ...

  8. beego——view 模板语法

    一.基本语法 go统一使用{{和}}作为左右标签,没有其它的标签符号. 使用"."来访问当前位置的上下文,使用"$"来引用当前模板根级的上下文,使用$var来访 ...

  9. 1kb的前端HTML模板解析引擎,不限于嵌套、循环、函数你能想到的解析方式

    传送门:https://github.com/xiangyuecn/BuildHTML copy之前说点什么 html做点小功能(什么都没有),如果是要手动生成html这种操作,容易把代码搞得乱七八糟 ...

随机推荐

  1. Markdown基础教程

    标题 Markdown支持6种级别的标题,对应html标签 h1 ~ h6

  2. Vue 使用 prerender-spa-plugin 添加loading

    主要配置代码: new PrerenderSPAPlugin({ staticDir: path.join(__dirname, 'dist'), routes: ['/', '/introducti ...

  3. ASP.NET程序发布

    详细流程请参考文章:https://www.cnblogs.com/wangjiming/p/6286045.html 主要补充个人操作过程中遇到的问题: 1)网站发布完成后,站点下没有aspnet_ ...

  4. spring事务详解(二)实例

    在Spring中,事务有两种实现方式: 编程式事务管理: 编程式事务管理使用底层源码可实现更细粒度的事务控制.spring推荐使用TransactionTemplate,典型的模板模式. 申明式事务管 ...

  5. Spring面试问答25题

    1.什么是Spring框架?Spring框架有哪些主要模块? Spring框架是一个为Java应用程序的开发提供了综合.广泛的基础性支持的Java平台.Spring帮助开发者解决了开发中基础性的问题, ...

  6. Linux中断(interrupt)子系统之一:中断系统基本原理【转】

    转自:http://blog.csdn.net/droidphone/article/details/7445825 这个中断系列文章主要针对移动设备中的Linux进行讨论,文中的例子基本都是基于AR ...

  7. windows下使用pip安装python模块lxml

    pip install lxml 1 1 会有如下问题:  结果一路解决下去,解决了一个坑还是有一个坑,遂放弃,查找有没有别的解决办法. 亲测使用wheel+pip可以成功安装lxml! wheel本 ...

  8. 分析占用了大量CPU处理时间的是Java进程中哪个线程

    下面是详细步骤: 1. 首先确定进程的 ID ,可以使用 jps -v 或者 top 命令直接查看 2. 查看该进程中哪个线程占用大量 CPU,执行 top -H -p [PID] 结果如下: 可以发 ...

  9. 003_cd pushd popd三个命令的区别

    一. It depends. In zsh you can configure cd to push the old directory on the directory stack automati ...

  10. 5 个 Laravel Eloquent 小技巧

    1. 快速生成 Model & Migration 这并不是一个很多人知道的小技巧,为文章生成 Model 和 Migration. $ php artisan make:migration ...