渲染一个带有传递参数的部分模板。

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

Render a partial template with passed parameters

问题

在Go语言中,可以使用html/template包来渲染模板并传递额外的参数。要在Go中实现类似Ruby中的部分模板渲染,可以按照以下步骤进行操作:

  1. 首先,创建一个包含部分模板的文件,例如partial1.tmpl
<div>
text1
{{if .Foo}}
   text2
{{end}}
</div>
  1. 在父模板文件parent.tmpl中,使用template指令来调用部分模板,并传递参数foo
<div>
  {{ template "partial1" . }}
</div>
  1. 在Go代码中,使用html/template包来解析和执行模板:
package main

import (
	"html/template"
	"os"
)

func main() {
	// 解析模板文件
	tmpl := template.Must(template.ParseFiles("parent.tmpl", "partial1.tmpl"))

	// 定义参数
	data := struct {
		Foo bool
	}{
		Foo: true,
	}

	// 执行模板并输出结果
	err := tmpl.Execute(os.Stdout, data)
	if err != nil {
		panic(err)
	}
}

在上述代码中,我们使用template.Must函数来解析模板文件,并使用template.Execute方法执行模板并将结果输出到标准输出。

注意,我们在执行模板时将参数data传递给了模板,其中Foo字段的值为true。这样,在部分模板中的{{if .Foo}}条件语句将会根据参数的值进行判断。

希望这可以帮助到你!如果你有任何其他问题,请随时问我。

英文:

I know rendering a partial template with additional parameters is possible in Ruby, how can I do it in Go?

I have a partial template _partial1.tmpl:

&lt;div&gt;
text1
{{if foo}}
   text2
{{end}}
&lt;/div&gt;

using it from the parent template parent.tmpl:

&lt;div&gt;
  {{ template &quot;partial1&quot;,  }} // how do I pass foo param here?? 
&lt;/div&gt;  

How do I pass the parameter foo to the partial?

答案1

得分: 5

文档中说明了template指令有两种形式:

{{template "name"}}
执行指定名称的模板,数据为nil。

{{template "name" pipeline}}
执行指定名称的模板,将dot设置为pipeline的值。

后一种形式接受一个pipeline语句,其值将被设置为执行模板中的dot值。因此,调用

{{template "partial1" "string1"}}

将在partial1模板中将{{.}}设置为"string1"。虽然无法在partial中设置名称为foo,但可以传递参数,并且它们将出现在.中。示例:

template.html

<div>
  {{ template "partial1.html" "muh" }} // 如何在这里传递foo参数??
</div>

partial1.html

{{if eq . "muh"}}
blep
{{else}}
moep
{{end}}

main.go

import (
	"html/template"
	"fmt"
	"os"
)

func main() {
	t, err := template.ParseFiles("template.html", "partial1.html")

    if err != nil { panic(err) }

	fmt.Println(t.Execute(os.Stdout, nil))
}

运行此程序将打印模板内容,并从partial中输出blep。更改传递的值将更改此行为。

您还可以分配变量,因此在partial中将.分配给foo是可能的:

{{ $foo := . }}
英文:

The documentation states that the template directive has two forms:

> {{template &quot;name&quot;}}
> The template with the specified name is executed
> with nil data.
>
> {{template &quot;name&quot; pipeline}}
> The template with the specified name is
> executed with dot set to the value of the pipeline.

The latter accepts a pipeline statement which's value is then set to the dot value in the executed template. So calling

{{template &quot;partial1&quot; &quot;string1&quot;}}

will set {{.}} to &quot;string1&quot; in the partial1 template. So while there is no way to set the name foo in the partial, you can pass parameters and they will appear in .. Example:

template.html

&lt;div&gt;
  {{ template &quot;partial1.html&quot; &quot;muh&quot;}} // how do I pass foo param here??
&lt;/div&gt;

partial1.html

{{if eq . &quot;muh&quot;}}
blep
{{else}}
moep
{{end}}

main.go

import (
	&quot;html/template&quot;
	&quot;fmt&quot;
	&quot;os&quot;
)

func main() {
	t,err := template.ParseFiles(&quot;template.html&quot;, &quot;partial1.html&quot;)

    if err != nil { panic(err) }

	fmt.Println(t.Execute(os.Stdout, nil))
}

Running this program will print the template's contents with blep from the partial. Changing the passed value will change this behaviour.

You can also assign variables, so assigning . to foo is possible in the partial:

{{ $foo := . }}

huangapple
  • 本文由 发表于 2016年3月28日 03:36:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/36251743.html
匿名

发表评论

匿名网友

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

确定