Error in go templates using if statement

In the main template I call the sub template:

<div id="nav">
      {{template "nav" ("posts")}}
</div>

In the sub template I have following if statement:

{{if eq . "posts"}}
       <a href="/posts" class="btn active"><img src="icn/post.svg"><p>test</p></a>
{{else}}
        <a href="/posts" class="btn"><img src="icn/post.svg"></a>
{{end}}

But I get an error when executing:

template: nav.html:3:5: executing “nav” at <eq . “posts”>: error calling eq: invalid type for comparison

What am I doing wrong? The argument “post” is transmitted to the sub template and visible in the sub template.

TIA

Works for me…

https://play.golang.org/p/5stR8sOknN0

1 Like

Yes. It works. The problem was that I called other “nav” without arguments.

Thank You!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.