beego的View編寫(xiě)

2023-11-20 18:04 更新

在前面編寫(xiě) Controller 的時(shí)候,我們?cè)?Get 里面寫(xiě)過(guò)這樣的語(yǔ)句 this.TplName = "index.tpl",設(shè)置顯示的模板文件,默認(rèn)支持 tpl 和 html 的后綴名,如果想設(shè)置其他后綴你可以調(diào)用 beego.AddTemplateExt 接口設(shè)置,那么模板如何來(lái)顯示相應(yīng)的數(shù)據(jù)呢?beego 采用了 Go 語(yǔ)言默認(rèn)的模板引擎,所以和 Go 的模板語(yǔ)法一樣,Go 模板的詳細(xì)使用方法請(qǐng)參考《Go Web 編程》模板使用指南

我們看看快速入門(mén)里面的代碼(去掉了 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}}" rel="external nofollow" target="_blank" >{{.Website}}</a>
                            <br />
                            Contact me: {{.Email}}
                        </p>
                    </div>
                </div>
            </div>
        </header>
    </body>
</html>

我們?cè)?Controller 里面把數(shù)據(jù)賦值給了 data(map 類型),然后我們?cè)谀0逯芯椭苯油ㄟ^(guò) key 訪問(wèn) .Website 和 .Email 。這樣就做到了數(shù)據(jù)的輸出。接下來(lái)我們講解如何讓靜態(tài)文件輸出。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)