错误:在revel模板中出现“无效的比较类型”

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

Error: "invalid type for comparison" in revel template

问题

在自定义footer.html后创建多个端点后,我遇到了这个错误,但没有明显的影响应用程序的功能,只是让我感到烦恼。

尝试过:

revel run revel_app 或 dev

Revel模板执行错误

: 在<eq .RunMode "dev">处执行"footer.html"时:调用eq时出错:比较的类型无效。

{{if eq .RunMode "dev"}}

{{template "debug.html" .}}

{{end}}
</body>

</html>

英文:

After creating multple endpoints customizing footer.html, I end up with this error for not apparent this don't effect the functionally of the application, just annoying me.
Tried:

  revel run revel_app or dev

Revel Template Execution Error

: executing "footer.html" at <eq .RunMode "dev">: error calling eq: invalid type for comparison.

  {{if eq .RunMode &quot;dev&quot;}}

  {{template &quot;debug.html&quot; .}}

  {{end}}
 &lt;/body&gt;

&lt;/html&gt;

答案1

得分: 4

虽然这个问题似乎已经得到了回答,但还有很多内容是缺失的。首先,问题是不完整的...错误是什么?我猜测在运行模板解析器/执行器时缺少了.RunMode。对于golang模板没有最佳实践,但是当模板过载并且没有维护变量字典时,这是一个常见问题。

我通常采用以下策略:

{{if eq (or .RunMode "default") "dev"}}

这样,如果.RunMode没有被赋值(空异常)或者是一个空字符串,那么eq就会使用"default"的值。可以将其视为类似于'C'中的简写方式。

这是一个夸张的例子。

a := runmode!=null?runmode:default
英文:

While this question seems to be answered there is plenty missing. First the question is incomplete... WHAT IS THE ERROR? My guess us that the .RunMode is missing when the template parser/executor was run. There are not best practices for golang templates, however, this is a common problem when overloading templates and not maintaining a dictionary of variables.

One strategy I tend to deploy is:

{{if eq (or .RunMode &quot;default&quot;) &quot;dev&quot;}}

This way if .RunMode is NOT assigned a value (nil exception) or an empty string then the eq uses the &quot;default&quot; value instead. Think of this as a shorthand like in 'C'

This is an exaggerated example.

a := runmode!=null?runmode:default

答案2

得分: 2

你正在将 "string" 与某个未知类型的变量 .RunMode 进行比较。将该变量转换为字符串类型如何?

{{if eq (.RunMode | toString) "dev"}}

英文:

You are comparing "string" with a variable .RunMode of some unknown type. How about casting that variable to string ?

{{if eq (.RunMode | toString) &quot;dev&quot;}}

答案3

得分: 0

你看到的错误是因为eq函数的一个参数在当前上下文中要么未定义,要么不是“基本类型”(请参考本节的最后一段)。

假设footer.html是一个与其他模板相关联的“部分”模板,这些模板使用模板操作调用页脚模板,你需要确保传递给模板调用的上下文中包含.RunMode的值,并且该值是一个基本类型

英文:

The error you see arises when one of the arguments to eq is either undefined in the current context or is not of a "basic type" (see the last paragraph of this section)

So assuming that footer.html is a "pratial" template that is associated with other templates which invoke the footer template using the template action you need to ensure that the context passed in to the template invocation contains the .RunMode value and that the value is of a basic type.

huangapple
  • 本文由 发表于 2017年5月13日 01:43:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/43943637.html
匿名

发表评论

匿名网友

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

确定