使用标准库可以在Go中嵌套模板吗? (Google App Engine) [英] Is it possible to have nested templates in Go using the standard library? (Google App Engine)

查看:147
本文介绍了使用标准库可以在Go中嵌套模板吗? (Google App Engine)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在python运行库中获得像Jin​​ja一样的嵌套模板。 TBC我的意思是我如何从一个基本模板继承一堆模板,只需填写基本模板块,如Jinja / django-templates就可以。是否可以在标准库中使用 html / template



如果这不可能,我的替代品。小胡子似乎是一种选择,但是我会错过 html / template 这些很好的细微特征,如上下文敏感的转义等。还有什么其他的选择?

(环境:Google App Engin,Go运行时v1,Dev - Mac OSx狮子)

感谢您的阅读。

是的,这是可能的。一个 html.Template 实际上是一组模板文件。如果你在这个集合中执行了一个定义的块,它可以访问在这个集合中定义的所有其他块。



如果你自己创建一个这样的模板集的映射,你基本上有Jinja / Django提供的灵活性。唯一的区别是 html / template 包不能直接访问文件系统,所以你必须自己解析和编写模板。



考虑以下两个不同页面(index.html和other.html)的例子,从base.html继承:

  // Base.html的内容:
{{definebase }}< HTML>
< head> {{templatehead。}}< / head>
< body> {{templatebody。}}< / body>
< / html> {{end}}

// index.html的内容:
{{definehead}}< title>索引< / title> ; {{end}}
{{definebody}} index {{end}}

// other.html的内容:
{{definehead }}< title>其他< / title> {{end}}
{{definebody}}其他{{end}}

以及以下映射模板集:

  tmpl:= make map [string] * template.Template)
tmpl [index.html] = template.Must(template.ParseFiles(index.html,base.html))
tmpl [ (template.ParseFiles(other.html,base.html))

您现在可以通过调用

  tmpl [index.html来呈现您的index.html ] .Execute(base,data)

,你可以渲染你的other.html页面通过调用

  tmpl [other.html]。执行(base,data)

使用一些技巧(例如一致的纳米ng模板文件的约定),甚至可以自动生成 tmpl 映射。


How do I get nested templates like Jinja has in the python runtime. TBC what I mean is how do I have a bunch of templates inherit from a base templates, just filing in blocks of the base templates, like Jinja/django-templates does. Is it possible using just html/template in the standard library.

If that is not a possibility, what are my alternatives. Mustache seems to be an option but would I then be missing out on those nice subtle features of html/template like the context sensitive escaping etc.? What other alternatives are ther?

(Environment: Google App Engin, Go runtime v1, Dev - Mac OSx lion)

Thanks for reading.

解决方案

Yes it is possible. A html.Template is actually a set of template files. If you execute a defined block in this set, it has access to all the other blocks defined in this set.

If you create a map of such template sets on your own, you have basically the same flexibility that Jinja / Django offers. The only difference is that the html/template package has no direct access to the file system, so you have to parse and compose the templates on your own.

Consider the following example with two different pages ("index.html" and "other.html") that both inherit from "base.html":

// Content of base.html:
{{define "base"}}<html>
  <head>{{template "head" .}}</head>
  <body>{{template "body" .}}</body>
</html>{{end}}

// Content of index.html:
{{define "head"}}<title>index</title>{{end}}
{{define "body"}}index{{end}}

// Content of other.html:
{{define "head"}}<title>other</title>{{end}}
{{define "body"}}other{{end}}

And the following map of template sets:

tmpl := make(map[string]*template.Template)
tmpl["index.html"] = template.Must(template.ParseFiles("index.html", "base.html"))
tmpl["other.html"] = template.Must(template.ParseFiles("other.html", "base.html"))

You can now render your "index.html" page by calling

tmpl["index.html"].Execute("base", data)

and you can render your "other.html" page by calling

tmpl["other.html"].Execute("base", data)

With some tricks (e.g. a consistent naming convention of your template files), it's even possible to generate the tmpl map automatically.

这篇关于使用标准库可以在Go中嵌套模板吗? (Google App Engine)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆