如何在Go中扩展一个模板?

huangapple go评论90阅读模式
英文:

How to extend a template in go?

问题

以下是翻译好的内容:

这是问题:每个页面的“content”部分有几个“article”,我想在每个文章下面插入一个“likebar”模板。

所以“base.tmpl”如下:

<html>
  <head>	
	{{template "head.tmpl" .}}
  </head>
  <body>   	
	{{template "content.tmpl" .}} 	
   </body>
</html>

在“article.tmpl”中,我想要:

{{define "content"}} 	
          <div>article 1 
	         {{template "likebar.tmpl" .}} 
          </div> 
          <div>article 2
     	     {{template "likebar.tmpl" .}} 
         </div>
       ... //这些div是动态生成的
    {{end}}

如何使用“html/template”实现这一点?
我尝试在“base.tmpl”中插入一个“{{template "iconbar" .}}”,然后在“{{define "content"}`中嵌套“{{template "likebar.tmpl" .}}”,但是出现了以下错误:

> 模板文件错误:html/template:base.tmpl:122:12: 没有名为“likebar.tmpl”的模板。

英文:

Here is the problem: There are several articles on each page's content section and I'd like to insert a likebar template below each article.

So the base.tmpl is like:

&lt;html&gt;
  &lt;head&gt;	
	{{template &quot;head.tmpl&quot; .}}
  &lt;/head&gt;
  &lt;body&gt;   	
	{{template &quot;content.tmpl&quot; .}} 	
   &lt;/body&gt;
&lt;/html&gt;

and in article.tmpl I want to have :

	{{define &quot;content&quot;}} 	
          &lt;div&gt;article 1 
	         {{template &quot;likebar.tmpl&quot; .}} 
          &lt;/div&gt; 
          &lt;div&gt;article 2
     	     {{template &quot;likebar.tmpl&quot; .}} 
         &lt;/div&gt;
       ... //these divs are generated dynamically
    {{end}}

How can I achieve this with html/template?
I have tried to insert a {{template &quot;iconbar&quot; .}} in base.tmpl and then nested {{template &quot;likebar.tmpl&quot; .}} inside {{define &quot;content&quot; but it failed with:

> Template File Error: html/template:base.tmpl:122:12: no such template
> "likebar.tmpl"

答案1

得分: 3

你只能包含/插入关联模板

如果你有多个模板文件,可以使用template.ParseFiles()template.ParseGlob()来解析它们,结果模板将包含所有已关联的模板,因此它们可以相互引用。

如果你确实使用上述函数来解析模板,那么找不到likebar.tmpl的原因是因为你使用了无效的名称引用它(例如,缺少文件夹名称)。

当从string源解析时,你可以使用Template.Parse()方法,它还将嵌套模板关联到顶级模板。

看看这两个可行的示例:

func main() {
    t := template.Must(template.New("").Parse(templ1))
    if err := t.Execute(os.Stdout, nil); err != nil {
        panic(err)
    }

    t2 := template.Must(template.New("").Parse(templ2))
    template.Must(t2.Parse(templ2Like))
    if err := t2.Execute(os.Stdout, nil); err != nil {
        panic(err)
    }
}

const templ1 = `Base template #1
And included one: {{template "likebar"}}
{{define "likebar"}}I'm likebar #1.{{end}}
`

const templ2 = `Base template #2
And included one: {{template "likebar"}}
`

const templ2Like = `{{define "likebar"}}I'm likebar #2.{{end}}`

输出结果(在Go Playground上尝试):

Base template #1
And included one: I'm likebar #1.

Base template #2
And included one: I'm likebar #2.
英文:

You can only include / insert associated templates.

If you have multiple template files, use template.ParseFiles() or template.ParseGlob() to parse them all, and the result template will have all the templates, already associated, so they can refer to each other.

If you do use the above functions to parse your templates, then the reason why it can't find likebar.tmpl is because you are referring to it by an invalid name (e.g. missing folder name).

When parsing from string source, you may use the Template.Parse() method, which also associates nested templates to the top level template.

See these 2 working examples:

func main() {
	t := template.Must(template.New(&quot;&quot;).Parse(templ1))
	if err := t.Execute(os.Stdout, nil); err != nil {
		panic(err)
	}

	t2 := template.Must(template.New(&quot;&quot;).Parse(templ2))
	template.Must(t2.Parse(templ2Like))
	if err := t2.Execute(os.Stdout, nil); err != nil {
		panic(err)
	}
}

const templ1 = `Base template #1
And included one: {{template &quot;likebar&quot;}}
{{define &quot;likebar&quot;}}I&#39;m likebar #1.{{end}}
`

const templ2 = `Base template #2
And included one: {{template &quot;likebar&quot;}}
`

const templ2Like = `{{define &quot;likebar&quot;}}I&#39;m likebar #2.{{end}}`

Output (try it on the Go Playground):

Base template #1
And included one: I&#39;m likebar #1.

Base template #2
And included one: I&#39;m likebar #2.

huangapple
  • 本文由 发表于 2016年11月21日 14:51:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/40714378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定