加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Golang Web 框架 Beego view 渲染 - 06

发布时间:2020-12-16 18:07:55 所属栏目:大数据 来源:网络整理
导读:View 编写 在前面编写 Controller 的时候,我们在 Get 里面写过这样的语句 this.TplName = “index.tpl”,设置显示的模板文件,默认支持 tpl 和 html 的后缀名,如果想设置其他后缀你可以调用 beego.AddTemplateExt 接口设置,那么模板如何来显示相应的数据

View 编写

在前面编写 Controller 的时候,我们在 Get 里面写过这样的语句 this.TplName = “index.tpl”,设置显示的模板文件,默认支持 tpl 和 html 的后缀名,如果想设置其他后缀你可以调用 beego.AddTemplateExt 接口设置,那么模板如何来显示相应的数据呢?beego 采用了 Go 语言默认的模板引擎,所以和 Go 的模板语法一样,Go 模板的详细使用方法请参考《Go Web 编程》模板使用指南

我们看看快速入门里面的代码(去掉了 css 样式):

<!DOCTYPE html> <html> <head> <title>Beego</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <header class="hero-unit" style="background-color:#A9F16C"> <div class="container"> <div class="row"> <div class="hero-text"> <h1>Welcome to Beego!</h1> <p class="description"> Beego is a simple & powerful Go web framework which is inspired by tornado and sinatra. <br /> Official website: <a href="http://{{.Website}}">{{.Website}}</a> <br /> Contact me: {{.Email}} </p> </div> </div> </div> </header> </body> </html>

我们在 Controller 里面把数据赋值给了 data(map 类型),然后我们在模板中就直接通过 key 访问 .Website 和 .Email 。这样就做到了数据的输出。接下来我们讲解如何让静态文件输出。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读