英文:
executing a template returns an unexpected <define> error
问题
在这段代码中,我正在尝试设置导航栏,并且我在每个文件中定义了nav
,因为对于某些文件,导航栏将是登录或注册,而对于其他文件,它将是注销或关于。除了这个login.html
文件之外,没有任何文件给我报错,报错信息如下:
错误
panic: template: login.html:7: unexpected <define> in command
代码
{{template "base" .}}
{{define "title"}} Log In {{end}}
{{define "body"}}
{{if .Loggedin}}
{{define "nav"}} // 这是显示错误的第7行。
<div>
<a href="/about">About</a>
<a href="/logout">Logout</a>
</div>
{{end}}
{{else}}
<h1> Log In</h1>
<p>Login to access your account.</p>
<hr>
<form action="/loggedin" method="POST" name="login" id="login">
<div>
<label for="email">Email</label>
<input type="email" name="email", placeholder="Enter your email address" required>
</div>
<div>
<label for="password">Password</label>
<input type="password" name="password", placeholder="Enter the password" required>
</div>
<div>
<input type="submit" value="Login">
</div>
</form>
{{end}}
{{end}}
英文:
In this code, I am trying to set the navbar and I am defining nav
in each file because for some files, the navbar will log in or signup but for some files, it will be log out or about. No file is giving me errors except this login.html
and the error is
Error
panic: template: login.html:7: unexpected <define> in command
code
{{template "base" .}}
{{define "title"}} Log In {{end}}
{{define "body"}}
{{if .Loggedin}}
{{define "nav"}} // this is line 7 which is showing error.
<div>
<a href="/about">About</a>
<a href="/logout">Logout</a>
</div>
{{end}}
{{else}}
<h1> Log In</h1>
<p>Login to access your account.</p>
<hr>
<form action="/loggedin" method="POST" name="login" id="login">
<div>
<label for="email">Email</label>
<input type="email" name="email", placeholder="Enter your email address" required>
</div>
<div>
<label for="password">Password</label>
<input type="password" name="password", placeholder="Enter the password" required>
</div>
<div>
<input type="submit" value="Login">
</div>
</form>
{{end}}
{{end}}
答案1
得分: 1
引用自text/template
包的文档,关于嵌套模板定义的部分:
模板定义必须出现在模板的顶层,就像Go程序中的全局变量一样。
你不能在另一个模板定义内部定义模板(而你正在这样做)。
另外请注意,{{define}}
只是定义一个模板,而不是包含/执行它。
要在原地定义和执行模板,请使用{{block}}
。
在你的情况下,将模板定义移到顶层,并使用{{template}}
操作在需要的地方执行它。
如果你需要根据某些条件定义不同的模板,那是不可能的。
可能的是定义不同的模板(具有不同的名称),并根据条件包含你需要的模板。
另一种选择是将一些数据(条件)传递给模板,并根据模板参数(条件)渲染不同的内容,例如使用{{if}}
操作。
有关更多选项,请参见相关链接:https://stackoverflow.com/questions/28830543/how-to-use-a-field-of-struct-or-variable-value-as-template-name/28831138#28831138
英文:
Quoting from package doc of text/template
, Nested template definitions:
> Template definitions must appear at the top level of the template, much like global variables in a Go program.
You can't define a template inside another template definition (and that's what you're doing).
Also note that {{define}}
just defines a template, it does not include / execute it.
To define and execute a template in place, use {{block}}
.
In your case move the template definition to the top level, and execute it where it's needed using the {{template}}
action.
If you need different template definitions based on certain conditions, that's not possible.
What's possible is define different templates (with different names), and include the one you need based on the conditions.
Another option is to pass some data (the conditions) to the template, and make it render different things based on the template parameters (the conditions) with e.g. using the {{if}}
action.
See related for even more options: https://stackoverflow.com/questions/28830543/how-to-use-a-field-of-struct-or-variable-value-as-template-name/28831138#28831138
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论