简体   繁体   中英

Error: “invalid type for comparison” in revel template

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 : error calling eq: invalid type for comparison.

  {{if eq .RunMode "dev"}}

  {{template "debug.html" .}}

  {{end}}
 </body>

</html>

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

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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM