如何访问模板范围内的会话变量?

I want to add an Edit button which appear only for moderators:

{{range $n := .articles}}
   <p>{{$n.Content}} </p>
   {{ if .is_mod}}
      <button> Edit </button>
   {{end}}
{{end }}

I have already set is_mod as a boolean variable in session and passed it to template. However, it is not a field in the Article struct, so, I get this error:

executing "content" at <.is_mod>: is_mod is not a field of struct type model.Article.

One obvious solution is to make a new struct in the controller which includes a IsMod field and pass that to template, but that is messy and inefficient so I'd rather avoid it if possible and looking for a more elegant solution.

You need to pass session variable to the template. I didn't tried it, but you can try something like this:

c.HMTL(http.StatusOK, "template_name", gin.H {
  "articles": articles,
  "is_mod": is_mod,
})

The above syntax is for gin-gonic/gin framework. .