英文:
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 "dev"}}
{{template "debug.html" .}}
{{end}}
</body>
</html>
答案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 "default") "dev"}}
This way if .RunMode
is NOT assigned a value (nil exception) or an empty string then the eq
uses the "default"
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) "dev"}}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论