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

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

Render a partial template with passed parameters

问题

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

  1. 首先,创建一个包含部分模板的文件,例如partial1.tmpl
  1. <div>
  2. text1
  3. {{if .Foo}}
  4. text2
  5. {{end}}
  6. </div>
  1. 在父模板文件parent.tmpl中,使用template指令来调用部分模板,并传递参数foo
  1. <div>
  2. {{ template "partial1" . }}
  3. </div>
  1. 在Go代码中,使用html/template包来解析和执行模板:
  1. package main
  2. import (
  3. "html/template"
  4. "os"
  5. )
  6. func main() {
  7. // 解析模板文件
  8. tmpl := template.Must(template.ParseFiles("parent.tmpl", "partial1.tmpl"))
  9. // 定义参数
  10. data := struct {
  11. Foo bool
  12. }{
  13. Foo: true,
  14. }
  15. // 执行模板并输出结果
  16. err := tmpl.Execute(os.Stdout, data)
  17. if err != nil {
  18. panic(err)
  19. }
  20. }

在上述代码中,我们使用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:

  1. &lt;div&gt;
  2. text1
  3. {{if foo}}
  4. text2
  5. {{end}}
  6. &lt;/div&gt;

using it from the parent template parent.tmpl:

  1. &lt;div&gt;
  2. {{ template &quot;partial1&quot;, }} // how do I pass foo param here??
  3. &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值。因此,调用

  1. {{template "partial1" "string1"}}

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

template.html

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

partial1.html

  1. {{if eq . "muh"}}
  2. blep
  3. {{else}}
  4. moep
  5. {{end}}

main.go

  1. import (
  2. "html/template"
  3. "fmt"
  4. "os"
  5. )
  6. func main() {
  7. t, err := template.ParseFiles("template.html", "partial1.html")
  8. if err != nil { panic(err) }
  9. fmt.Println(t.Execute(os.Stdout, nil))
  10. }

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

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

  1. {{ $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

  1. {{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

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

partial1.html

  1. {{if eq . &quot;muh&quot;}}
  2. blep
  3. {{else}}
  4. moep
  5. {{end}}

main.go

  1. import (
  2. &quot;html/template&quot;
  3. &quot;fmt&quot;
  4. &quot;os&quot;
  5. )
  6. func main() {
  7. t,err := template.ParseFiles(&quot;template.html&quot;, &quot;partial1.html&quot;)
  8. if err != nil { panic(err) }
  9. fmt.Println(t.Execute(os.Stdout, nil))
  10. }

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:

  1. {{ $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:

确定