模板中的变量在包含的模板中。

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

Variable in template's included template

问题

我正在尝试将值放入一个名为"header"的模板中,比如标题和导航链接,但无法从被包含的模板中访问我从主模板发送的变量。

渲染模板的代码如下:

...
templateName := "index"
args := map[string]string{
    "Title": "主页",
    "Body":  "这是内容",
}
PageTemplates.ExecuteTemplate(w, templateName+".html", args)
...

index.html模板如下:

{{template "header"}} <!-- 包含"header.html"模板 -->
{{.Body}}             <!-- 正常工作的变量 -->
{{template "footer"}} <!-- 不重要 -->

header.html模板如下:

{{define "header"}}
<!DOCTYPE html>
<html lang="en">
<head>
    <title>{{.Title}}</title> <!-- 变量为空 :(
</head>
<body>
{{end}}

显然,这种方式行不通。

也许有一种方法可以解析/获取模板,并将我的变量放入其中,而不需要将整个头文件放入代码中?然后我可以将该模板作为变量发送到我的主模板。但这似乎不是最好的方法。

英文:

I am trying to put values into a "header" template, like the title and navigation links but can't access the variables that I sent to the main template from the included one.

Rendering the template:

    ...
    templateName := &quot;index&quot;
   	args := map[string]string{
        	&quot;Title&quot;:       &quot;Main Page&quot;,
	        &quot;Body&quot;:        &quot;This is the content&quot;,
    }
    PageTemplates.ExecuteTemplate(w, templateName+&quot;.html&quot;, args)
    ...

index.html template:

   {{template &quot;header&quot;}} &lt;-- Including the &quot;header.html&quot; template
   {{.Body}}             &lt;-- Variable that works
   {{template &quot;footer&quot;}} &lt;-- Does not matter!

header.html template:

   {{define &quot;header&quot;}}
   &lt;!DOCTYPE html&gt;
   &lt;html lang=&quot;en&quot;&gt;
   &lt;head&gt;
            &lt;title&gt;{{.Title}}&lt;/title&gt; &lt;-- Variable empty :(
   &lt;/head&gt;
   &lt;body&gt;
   {{end}}

Apparently, it won't work that way.

Maybe there's a way I could parse/get the template and put my variables into it without putting the whole header file into code? Then I could just send that template as a variable to my main template. But that does not seem like it would be the best way to do it.

答案1

得分: 4

你可以在调用模板时将上下文传递给它。在你的例子中,将{{template "header"}}更改为{{template "header" .}}应该就足够了。

官方文档中相关的部分如下:

{{template "name"}}
使用空数据执行指定名称的模板。

{{template "name" pipeline}}
使用管道的值将dot设置为指定名称的模板的值。

PS:这与问题无关,但你还应该删除{{define "header"}}<!DOCTYPE html>之间的换行符,这样doctype才会真正成为模板中的第一件事。

英文:

You can pass the context to the template when you call it. In your example, changing {{template &quot;header&quot;}} to {{template &quot;header&quot; .}} should be sufficient.

The relevant parts from the official docs:

> {{template "name"}}<br>
> The template with the specified name is executed with nil data.
>
> {{template "name" pipeline}}<br>
> The template with the specified name is executed with dot set to the value of the pipeline.

PS: It is not relevant to the question, but you should also remove the newline between {{define &quot;header&quot;}} and &lt;!DOCTYPE html&gt;, so that the doctype is really the first thing in your template.

huangapple
  • 本文由 发表于 2013年8月8日 22:32:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/18128752.html
匿名

发表评论

匿名网友

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

确定