使用多个管道参数调用模板 [英] Calling a template with several pipeline parameters
问题描述
在Go模板中,有时将正确的数据传递给正确的模板的方式对我来说很尴尬。使用管道参数调用模板看起来像只用一个参数调用一个函数。
比方说,我有一个关于Gophers的Gophers网站。它有一个主页主模板和一个用于打印Gophers列表的实用程序模板。
http://play.golang.org/p/Jivy_WPh16
输出:
*伟大的GopherBook *(以杜威登录)
[最受欢迎]
>> Huey
>>杜威
>> Louie
[最活跃]
>> Huey
>> Louie
[最近]
>> Louie
现在我想在子模板中添加一些上下文:格式名称Dewey在列表中,因为它是当前记录的用户的名称。但我无法直接传递名称,因为只有一个可能点参数管道!我能做什么?
- 很明显,我可以将子模板代码复制粘贴到主模板中(我不想这样做,因为它会抛弃所有的兴趣一个子模板)。
- 或者我可以使用访问器来处理某种全局变量(我也不想)。
- 或者I可以为每个模板参数列表创建一个新的特定结构类型(不是很好)。 您可以在模板中注册一个字典函数,您可以使用该函数将多个值传递给模板调用。这个调用本身就像这样:
{{templateuserlistdictUsers.MostPopularCurrent.CurrentUser }}
小代码dict助手的代码,包括将其注册为模板func在这里:
var tmpl = template.Must(template.New()。Funcs(template.FuncMap {
dict:func(values ... interface {})(map [string] interface {},错误){
如果len(值)%2!= 0 {
return nil,errors。新(无效字典呼叫)
}
dict:= make(map [string] interface {},len(values)/ 2)
for i:= 0; i< len(values); i + = 2 {
key,ok:= values [i]。(string)
if!ok {
return nil,errors.New(dict keys must be字符串)
}
dict [key] = values [i + 1]
}
return dict,nil
},
})。ParseGlob (templates / *。html)
In a Go template, sometimes the way to pass the right data to the right template feels awkward to me. Calling a template with a pipeline parameter looks like calling a function with only one parameter.
Let's say I have a site for Gophers about Gophers. It has a home page main template, and a utility template to print a list of Gophers.
http://play.golang.org/p/Jivy_WPh16
Output :
*The great GopherBook* (logged in as Dewey) [Most popular] >> Huey >> Dewey >> Louie [Most active] >> Huey >> Louie [Most recent] >> Louie
Now I want to add a bit of context in the subtemplate : format the name "Dewey" differently inside the list because it's the name of the currently logged user. But I can't pass the name directly because there is only one possible "dot" argument pipeline! What can I do?
- Obviously I can copy-paste the subtemplate code into the main template (I don't want to because it drops all the interest of having a subtemplate).
- Or I can juggle with some kind of global variables with accessors (I don't want to either).
- Or I can create a new specific struct type for each template parameter list (not great).
解决方案You could register a "dict" function in your templates that you can use to pass multiple values to a template call. The call itself would then look like that:
{{template "userlist" dict "Users" .MostPopular "Current" .CurrentUser}}
The code for the little "dict" helper, including registering it as a template func is here:
var tmpl = template.Must(template.New("").Funcs(template.FuncMap{ "dict": func(values ...interface{}) (map[string]interface{}, error) { if len(values)%2 != 0 { return nil, errors.New("invalid dict call") } dict := make(map[string]interface{}, len(values)/2) for i := 0; i < len(values); i+=2 { key, ok := values[i].(string) if !ok { return nil, errors.New("dict keys must be strings") } dict[key] = values[i+1] } return dict, nil }, }).ParseGlob("templates/*.html")
这篇关于使用多个管道参数调用模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!