jjzjj

Template

全部标签

go - 如何加载和缓存 100 页模板,并呈现正确的模板并在处理程序中返回

我有100个模板需要在Web应用程序中使用。有没有一种方法可以将它们解析一次并重新使用它们,而不是为每个请求加载它们?现在假设模板不接受模型,它们只是静态模板(以使其更简单)。模板存储在下面的文件夹结构中,因此根据主题变量我将知道从哪里获取模板。/views/{theme}/index.tmpl到目前为止,我的处理程序是这样的:funcRenderPage(whttp.ResponseWriter,r*http.Request){vartheme:=//settemplatethemebasedonsomecondition}我如何预加载所有模板,然后以某种方式获取正确的模板并将其呈现

templates - Golang 模板从字符串数组生成值错误

我正在使用需要生成以下内容的Golang项目app1&&app2&&app3我的模板如下所示{{-rangeExeApp.}}{{.Command}}{{-end}}我的代码看起来像下面的命令,它是字符串数组typeAppstruct{DatastringCommand[]string}//ThisisthefunctionfuncExeApp(mmodels)[]App{switchm.Type{case“apps":return[]App{{"#runningapps",[]string{“app1",“app2",“app3"}},}…目前它生成的像[app1app2app3]我

go - {{template "name"pipeline}} 是什么意思

这个问题在这里已经有了答案:Golangtemplateenginepipelines(1个回答)关闭4年前。在https://golang.org/pkg/text/template/#hdr-Actions,有如下解释{{template"name"pipeline}}Thetemplatewiththespecifiednameisexecutedwithdotsettothevalueofthepipeline.这是什么意思?什么是点?例如,我看到下面的模板代码——{{define"header"}}{{template"top".}}{{template"needs"}}..

go - 是否可以在带有 go template 的模板中使用模板

使用https://golang.org/pkg/text/template/,我有时需要在访问路径中使用变量(对于kubernetes部署)。我最后写的是这样的:{{if(eq.Values.cluster"aws"}}{{.Values.redis.aws.masterHost|quote}}{{else}}{{.Values.redis.gcp.masterHost|quote}}{{end}}我真正想写的是{{.Values.redis.{{.Values.cluster}}.masterHost|quote}},无法编译。有没有办法写类似的东西?(因此在访问路径中有一种变量)

go - 如何将命名模板的结果发送到函数

这个问题在这里已经有了答案:Captureorassigngolangtemplateoutputtovariable(1个回答)getthevalueofagotemplatefrominsideanothertemplate[duplicate](1个回答)关闭3年前。我正在尝试缩进命名模板的结果。我已经尝试了以下所有语法。“模板名称”两边的括号。也不行。{{template"my-org.labels".|indent8}}{{indent8template"mbfs-postgres.labels".}}{{withtemplate"mbfs-postgres.labels".

go - 通过 html/template 删除传递参数周围的空格

当我将参数传递给onclick函数时,我在该参数周围有空格,为什么以及如何删除它们?t,_:=template.New("").Parse(`{{.}}`)t.Execute(os.Stdout,1)结果:1playground编辑:由Dave帮助更新,从模板我们可以做这样的事情:t,_:=template.New("").Funcs(template.FuncMap{"test":func(iinterface{})template.JS{switchi.(type){caseint:s:=strconv.Itoa(i.(int))returntemplate.JS(s)//othe

go - 匿名结构作为模板中的管道

有没有办法在html/template中执行以下操作?{{template"mytemplate"struct{Foo1,Foo2string}{"Bar1","Bar2"}}}实际上在模板中,如上。不是通过在返回结构的FuncMap中注册的函数。我试过了,但是Parse崩溃了,seePlayground.也许只是语法错误? 最佳答案 正如其他人所指出的,这是不可能的。模板在运行时解析,无需Go编译器的帮助。因此,允许任意Go语法是不可行的(尽管请注意,这并非不可能,因为标准库包含解析Go源文本的所有工具,请参阅标准库中“前缀为”g

go - 如何将 web 模板变量设置为动态 html 和 golang 代码?

我在golang上有两个网页,我想将这个页面代码嵌入到{{.content}}变量(在templates/main.html中定义),根据即将到来的请求动态变化。例如,如果客人进入用户注册页面,我希望{{.content}}变量将是用户注册代码,否则是用户配置文件代码。templates/userregister.html页面代码;{{define"userregister"}}...{{.specialmessage}}...{{end}}templates/userprofile.html页面代码;{{define"userprofile"}}...{{.specialmessag

go - 为什么 IO.Writer 不填充接收器?

我正在尝试测试模板生成工具。为了做到这一点,我认为捕获模板执行输出的最简单方法是使用iowriter并在测试期间提供它。问题是由于某些原因,接收器没有使用模板输出“更新”。希望下面的代码能更好地解释我面临的问题。packagemainimport"fmt"import"text/template"typeCompanystruct{Namestring}typeCompanies[]Companyfuncmain(){s:=new(stringer)v:=Companies{Company{Name:"Sony"}}tmp:=template.Must(template.New("ma

go - 文本/模板 : "can' t call method/function with 0 results. "

如何在不返回任何值的模板中执行函数?这是示例:funcmain(){u,err:=url.Parse("http://example.com/test?param1=true¶m2=true")iferr!=nil{log.Fatal(err)}m:=u.Query()m.Del("param1")//param1successfuldeleted!u.RawQuery=m.Encode()fmt.Println(u.RawQuery)consttmpl=`{{$m:=.Query}}{{$m.Del"param2"}}{{.RawQuery}}`t:=template.Mus