草庐IT

template-toolkit

全部标签

templates - 有没有办法列出使用过的变量?

假设我有一个基本的go文本/模板:{{.var}}isanother{{.var2}}我想获取模板中使用的变量名称的数组,以便能够在我传递给执行的数据中不可用时跳过执行,是否可以通过某种方式做到这一点?因为我的数据不是结构而是映射,所以执行.var总是会返回一些东西:如果它不存在,当我希望在执行模板时出现错误时它会返回一个空字符串。所以对于上面的例子,我希望得到:[varvar2] 最佳答案 如果未设置值,请使用返回错误的模板函数。像这样:template.FuncMap(map[string]interface{}{"requir

go - 如何使用text/template预定义的 "call"函数?

我试图了解如何在text/template包中使用call函数。这是示例:typeHumanstruct{Namestring}func(h*Human)Say(strstring)string{returnstr}funcmain(){constletter=`{{.Name}}wantstosay{{"blabla"|.Say}}{{.Name}}wantstryagain,{{call.Say"blabla"}}.`varh=&Human{"Tim"}t:=template.Must(template.New("").Parse(letter))err:=t.Execute(os

templates - text/html 模板包中的 "range"操作和 "pipeline"说明。戈朗

我试图在text/html模板包中获得一些优点。我已经从golang站点阅读了它的文档。很难理解.(点)在一般和一定时间范围内行动。“pipeline”到底是什么意思,可能因为我的英文不是母语,所以比较难理解):{{pipeline}}Thedefaulttextualrepresentationofthevalueofthepipelineiscopiedtotheoutput.让我们考虑一个例子:data:=map[string]interface{}{"struct":&Order{ID:1,CustID:2,Total:3.65,Name:"Something",},"name

templates - Golang 模板和有效字段测试

在Go的database/sql包中,有一堆Null[Type]结构可以帮助将数据库值(及其可能的空值)映射到代码中。我试图弄清楚如何测试结构field是否为null,或者换句话说,当它的Valid属性为false时。打印SQL字段的推荐方法是使用.Value属性,如下所示:{{.MyStruct.MyField.Value}}效果很好。但假设我有一些稍微复杂的东西,我需要根据其他东西来测试值,例如:{{range.SomeSlice}}{{.}}{{end}}碰巧,这也很有效,除非.MyField无效,在这种情况下我会收到错误消息“调用eq时出错:用于比较的类型无效”。该错误是有道理

http - Golang使用 "template"包生成动态网页给客户端耗时太长

使用template包生成动态网页给客户端时太慢了。测试代码如下,golang1.4.1http.Handle("/js/",(http.FileServer(http.Dir(webpath))))http.Handle("/css/",(http.FileServer(http.Dir(webpath))))http.Handle("/img/",(http.FileServer(http.Dir(webpath))))http.HandleFunc("/test",TestHandler)funcTestHandler(whttp.ResponseWriter,r*http.Re

templates - 如何在 Go 模板中解析超出范围的变量?

我有如下两个结构,我需要使用templates在模板上呈现数据盒。我得到这个错误:EmailisnotafieldofstructtypeNotes.问题似乎是范围循环中似乎只有范围结构的字段可用,所以我想知道如何从范围结构外部(例如电子邮件字符串)导入字段。该行为非常出乎意料。typenotesstruct{Notestringsfstring}typeuisstruct{notes[]NoteEmailstring}varuiuisHTML{{range.notes}}{{.Email}}{{.sf}}{{end}}Email{{.Email}}我检查了godocs,但它们似乎毫无

templates - 戈朗。如何使用 html/template 包创建循环函数

我正在尝试将循环实现为自定义函数。它需要迭代次数和大括号之间的内容,然后它应该迭代括号之间的内容n次。请看例子:ma​​in.gotemplate.Must(template.ParseFiles("palette.html")).Funcs(template.FuncMap{"loop":func(nint,contentstring)string{varrstringfori:=0;iindex.html{{define"index"}}{{loop16}}{{end}}{{end}}输出...16times是否可以实现?其动机是text/template的标准功能不允许仅在大括号

html - 为什么 html/template 不显示所有 html 条件注释?

我有一个简单的GoHTML模板,其中包含HTML条件注释:packagemainimport("html/template""os")varbody=``funcmain(){tmp:=template.Must(template.New("tmp").Parse(body))tmp.Execute(os.Stdout,nil)}Thisproduces:为什么html/template编译后删除那些条件注释? 最佳答案 我的解决方法是重新实现在提交时删除的noescape助手#938597eab997funcMap:=templa

戈朗 : Parse all templates in directory and subdirectories?

这是我的目录结构:app/template/layout/base.tmplindex.tmpltemplate.ParseGlob("*/*.tmpl")解析index.tmpl但不解析中的base.tmpllayout子目录。有没有办法递归解析所有模板? 最佳答案 不是没有实现你自己的功能来做到这一点,我一直在使用这样的东西funcParseTemplates()*template.Template{templ:=template.New("")err:=filepath.Walk("./views",func(pathstri

templates - Golang 从文件中嵌入 html

如果我有这样的HTML文件,我该如何在Golang中做:{{.Header}}我想将一部分代码嵌入到另一个文件的header标签中,如下所示:我的尝试:header,_:=template.ParseFiles("header.html")c:=Content{Header:""}header.Execute(c.Header,nil)index:=template.Must(template.ParseFiles("index.html"))index.Execute(w,c) 最佳答案 如果您使用template.ParseFi