执行模板返回意外的<define>错误。

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

executing a template returns an unexpected <define> error

问题

在这段代码中,我正在尝试设置导航栏,并且我在每个文件中定义了nav,因为对于某些文件,导航栏将是登录或注册,而对于其他文件,它将是注销或关于。除了这个login.html文件之外,没有任何文件给我报错,报错信息如下:

错误

panic: template: login.html:7: unexpected &lt;define&gt; in command

代码

{{template &quot;base&quot; .}}

{{define &quot;title&quot;}} Log In {{end}}

{{define &quot;body&quot;}}
    {{if .Loggedin}}
        {{define &quot;nav&quot;}}  // 这是显示错误的第7行。
            &lt;div&gt;
                &lt;a href=&quot;/about&quot;&gt;About&lt;/a&gt;
                &lt;a href=&quot;/logout&quot;&gt;Logout&lt;/a&gt;
            &lt;/div&gt;
        {{end}}
    {{else}}
        &lt;h1&gt; Log In&lt;/h1&gt;    
        &lt;p&gt;Login to access your account.&lt;/p&gt;
        &lt;hr&gt;
        &lt;form action=&quot;/loggedin&quot; method=&quot;POST&quot; name=&quot;login&quot; id=&quot;login&quot;&gt;
            &lt;div&gt;
                &lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;
                &lt;input type=&quot;email&quot; name=&quot;email&quot;, placeholder=&quot;Enter your email address&quot; required&gt;
            &lt;/div&gt;
            &lt;div&gt;
                &lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;
                &lt;input type=&quot;password&quot; name=&quot;password&quot;, placeholder=&quot;Enter the password&quot; required&gt;
            &lt;/div&gt;
            &lt;div&gt;
                &lt;input type=&quot;submit&quot; value=&quot;Login&quot;&gt;
            &lt;/div&gt;
        &lt;/form&gt;
    {{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 &lt;define&gt; in command

code

{{template &quot;base&quot; .}}

{{define &quot;title&quot;}} Log In {{end}}

{{define &quot;body&quot;}}
    {{if .Loggedin}}
        {{define &quot;nav&quot;}}  // this is line 7 which is showing error.
            &lt;div&gt;
                &lt;a href=&quot;/about&quot;&gt;About&lt;/a&gt;
                &lt;a href=&quot;/logout&quot;&gt;Logout&lt;/a&gt;
            &lt;/div&gt;
        {{end}}
    {{else}}
        &lt;h1&gt; Log In&lt;/h1&gt;    
        &lt;p&gt;Login to access your account.&lt;/p&gt;
        &lt;hr&gt;
        &lt;form action=&quot;/loggedin&quot; method=&quot;POST&quot; name=&quot;login&quot; id=&quot;login&quot;&gt;
            &lt;div&gt;
                &lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;
                &lt;input type=&quot;email&quot; name=&quot;email&quot;, placeholder=&quot;Enter your email address&quot; required&gt;
            &lt;/div&gt;
            &lt;div&gt;
                &lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;
                &lt;input type=&quot;password&quot; name=&quot;password&quot;, placeholder=&quot;Enter the password&quot; required&gt;
            &lt;/div&gt;
            &lt;div&gt;
                &lt;input type=&quot;submit&quot; value=&quot;Login&quot;&gt;
            &lt;/div&gt;
        &lt;/form&gt;
    {{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

huangapple
  • 本文由 发表于 2021年7月30日 19:45:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/68590430.html
匿名

发表评论

匿名网友

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

确定