【问题标题】:golang output templatesgolang 输出模板
【发布时间】:2012-04-17 06:01:47
【问题描述】:

如何显示模板的内容?

主包

import (
    "fmt"
    "html/template"
    "os"

)

func main() {
    t := template.New("another")
    t,e:=t.ParseFiles("test.html")
    if(e!=nil){
            fmt.Println(e);
    }
    t.Execute(os.Stdout, nil)

}

为什么不呢? test.html 存在

【问题讨论】:

  • 文字啦啦啦啦啦啦啦啦

标签: templates go


【解决方案1】:

您无需使用New 创建新模板,然后在其上使用ParseFiles。还有一个函数 ParseFiles 负责在后台创建新模板。
这是一个示例:

package main

import (
    "fmt"
    "html/template"
    "os"
)

func main() {
    t, err := template.ParseFiles("test.html")
    if err != nil {
            fmt.Println(err);
    }
    t.Execute(os.Stdout, nil)
}

【讨论】:

    猜你喜欢
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多