多个具有FuncMap的模板

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

Multiple templates with FuncMap

问题

目标:在HTTP服务器中使用多个模板,我想将一些字符串中的换行符转换为<br/>标签。

一个简化的示例:

我有两个模板a.tmplb.tmpl,内容如下:

模板a {{dosomething}}

(其他模板类似)。它们都位于名为templates的目录中。我认为我需要创建一个函数来进行\n<br />的替换(上面的dosomething)。

这是我的(不工作的)示例代码:

package main

import (
    "log"
    "text/template"
)

func main() {
    // funcMap := template.FuncMap{
    //     "dosomething": func() string { return "done something" },
    // }

    templates, err := template.ParseGlob("templates/*.tmpl")
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("%#v", templates)

}

错误信息是:

2013/03/04 20:08:19 template: a.tmpl:1: function "dosomething" not defined
exit status 1

这是有道理的,因为在解析时,函数dosomething是未知的。

  1. 我如何在多个模板中使用我的函数?是否只能按照这里的问题中的答案进行操作?
  2. 这是正确的方法吗?请记住,我想要更改一些字符串的文本,类似于文档中的标题示例(http://golang.org/pkg/text/template/#FuncMap - Example (Func))。
  3. 如何在以下代码中访问b.tmpl

<!-- -->

package main

import (
    "log"
    "text/template"
)

func main() {
    funcMap := template.FuncMap{
        "dosomething": func() string { return "done something" },
    }

    t, err := template.New("a.tmpl").Funcs(funcMap).ParseGlob("templates/*.tmpl")
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("%#v", t)
}
英文:

The Goal: using multiple templates in an HTTP server where I want to change newlines into &lt;br/&gt; tags on some strings.

A stripped down example:

I have two templates a.tmpl and b.tmpl which look like this:

Template a {{dosomething}}

(and similar the other template). Both reside in a directory called templates. I believe that I need to create a function to do the \n -> &lt;br /&gt; replacement (dosomething above).

This is my (non working) sample code:

package main

import (
	&quot;log&quot;
	&quot;text/template&quot;
)

func main() {
	// funcMap := template.FuncMap{
	// 	&quot;dosomething&quot;: func() string { return &quot;done something&quot; },
	// }

	templates, err := template.ParseGlob(&quot;templates/*.tmpl&quot;)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf(&quot;%#v&quot;, templates)

}

The error message is:

2013/03/04 20:08:19 template: a.tmpl:1: function &quot;dosomething&quot; not defined
exit status 1

which makes sense, because during the parsing time, the function dosomething is not known.

  1. How can I use my function in multiple templates? Is the answer to this question here on so the only way to go?

  2. Is this the correct approach? Remember, I'd like to change the text on some strings, similar to the title example in the documentation (http://golang.org/pkg/text/template/#FuncMap - Example (Func))?

  3. How do I access b.tmpl in the following code:
    <!-- -->

     package main
    
     import (
     	&quot;log&quot;
     	&quot;text/template&quot;
     )
    
     func main() {
     	funcMap := template.FuncMap{
     		&quot;dosomething&quot;: func() string { return &quot;done something&quot; },
     	}
    
     	t, err := template.New(&quot;a.tmpl&quot;).Funcs(funcMap).ParseGlob(&quot;templates/*.tmpl&quot;)
     	if err != nil {
     		log.Fatal(err)
     	}
     	log.Printf(&quot;%#v&quot;, t)
     }
    

答案1

得分: 3

你最后的代码片段看起来没问题。

要渲染b.tmpl,只需调用

t.ExecuteTemplate(w, "b.tmpl", data)

你可以以同样的方式访问a.tmpl;为了保持一致性,我建议这样做,而不是在调用New时将名称设置为"a.tmpl"。

英文:

Your last snippet of code looks about right to me.

To render b.tmpl, just call

t.ExecuteTemplate(w, &quot;b.tmpl&quot;, data)

You can access a.tmpl the same way; I would recommend doing this for consistency, rather than setting the name to "a.tmpl" in the call to New.

huangapple
  • 本文由 发表于 2013年3月5日 03:14:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/15209236.html
匿名

发表评论

匿名网友

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

确定