英文:
How can I add FuncMaps to already parsed templates in golang
问题
我有一些模板,最初在应用程序启动时进行解析(显然是为了加快速度)。
var templates = template.New("template")
filepath.Walk("views", func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".html") {
templates.ParseFiles(path)
}
return nil
})
log.Println("Templates Parsed")
然后,我有一个函数映射,它们在自己的函数中添加(因为我需要请求对象,以便获取会话数据,如下所示)。
func View(w http.ResponseWriter, r *http.Request, tmplN string, data interface{}) {
tmpl := templates.Funcs(template.FuncMap{
"username": func() string {
session := Sesh(r)
username := ""
if session.Values["username"] != nil {
username = session.Values["username"].(string)
}
return username
},
"authenticated": func() bool {
session := Sesh(r)
authenticated := false
if session.Values["authenticated"] != nil {
authenticated = session.Values["authenticated"].(bool)
}
return authenticated
},
})
err := tmpl.ExecuteTemplate(w, tmplN, data)
if err != nil {
log.Println("Error " + err.Error())
}
}
但是,如果我不在模板解析之前调用Funcs
函数,它似乎不起作用,例如,如果我尝试在我的register
模板中使用如下所示:
{{ define "register" }}
{{ template "_header" .}}
{{ if authenticated }}
// 调用 FuncMap 函数
{{ end }}
<br/>
<br/>
<br/>
<div class="row align-center">
<div class="large-4 columns text-center">
<div id="RegistrationFormComponent"></div>
</div>
</div>
{{ template "_footer" .}}
{{ end }}
我会得到一个错误,提示“register”不存在,因为当它尝试解析时,函数authenticated
会抛出一个错误。如果有关如何使其按预期工作的任何信息,将非常感谢。
英文:
I have my templates which are initially parsed on the app launch (obviously for speed reasons like this)
var templates = template.New("template")
filepath.Walk("views", func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".html") {
templates.ParseFiles(path)
}
return nil
})
log.Println("Templates Parsed")
Then I have my funcmaps which get added in their own function (because I need the request object so I can get their session data like so)
func View(w http.ResponseWriter, r *http.Request, tmplN string, data interface{}) {
tmpl := templates.Funcs(template.FuncMap{
"username": func() string {
session := Sesh(r)
username := ""
if session.Values["username"] != nil {
username = session.Values["username"].(string)
}
return username
},
"authenticated": func() bool {
session := Sesh(r)
authenticated := false
if session.Values["authenticated"] != nil {
authenticated = session.Values["authenticated"].(bool)
}
return authenticated
},
})
err := tmpl.ExecuteTemplate(w, tmplN, data)
if err != nil {
log.Println("Error " + err.Error())
}
}
But it seems like if I don't put the Funcs
call before the parsing of the templates it doesn't work, e.g. if I try to use in my register
template like so:
{{ define "register" }}
{{ template "_header" .}}
{{ if authenticated }}
// Call FuncMap function
{{ end }}
<br/>
<br/>
<br/>
<div class="row align-center">
<div class="large-4 columns text-center">
<div id="RegistrationFormComponent"></div>
</div>
</div>
{{ template "_footer" .}}
{{ end }}
I get an error that "register" does not exist because the function authenticated
throws an error when it's trying to parse it. Any information on how I can get this to work as intended would be great thanks.
答案1
得分: 2
所以我找到了解决方法,但我会将这个答案留在这里,因为似乎没有地方回答,基本上我可以定义一个冗余的FuncMap
,模仿我将在会话中使用的那个,并且只返回空白,然后我可以用我的view
函数中的FuncMap
(在问题帖子中可见)覆盖它们,像这样:
var templates = template.New("template").Funcs(template.FuncMap{
"authenticated": func() bool {
log.Println("Was I called?")
return false
},
"username": func() string {
return ""
},
})
英文:
So I figured it out, but I'll leave this answer here as it seems to not be answered anywhere, basically I can define a redundant FuncMap
that mimics the one I will be using with the sessions, and just return them blank, I can then overwrite them with the FuncMap
from my view
function (visible in the question post) like this:
var templates = template.New("template").Funcs(template.FuncMap{
"authenticated": func() bool {
log.Println("Was I called?")
return false
},
"username": func() string {
return ""
},
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论