• gin version (or commit ref): 1.3
  • git version:
  • operating system: ubuntu 18.04

Description

https://stackoverflow.com/questions/52008320/go-gin-variable-in-handler-is-not-passing-to-template

Comment From: isgj

From the code you have posted

 render(c, gin.H{"title": "Successful Login"}, "login-successful.html")

you are not passing the is_logged_in, so the second section if not ... will render.

Comment From: tomriddle1234

@isgj ,yes because by default a new variable "is_logged_in" 's opposite is true. It is actually the variable didn't pass to template with just .Set() ?

Comment From: isgj

No, the template can see only what you pass to the c.HTML(...) (see method). What you set with c.Set(...) can be seen only by the middleware and the handler.

Comment From: GwynethLlewelyn

Piping in late, but, besides thanking @isgj for explaining how Gin actually works and deals with these variables, I would suggest that this is turned into a feature request, for one simple reason: if you have just one or two templates it's easy to add all those extra variables; once you start developing a very complex site with lots of different templates, called from different places, then it's easy to miss one or two c.HTML(...) calls... I tried to add middleware to automatically add whatever variables I had to all router calls automatically, but, alas, this doesn't work, either (for precisely the same reason).

Comment From: barats

Is there any resolution for this issue yet? Or is there any other better approach for this kind of thing?

Comment From: GwynethLlewelyn

@barats I'd say this is not really an issue any longer. You can see from the earlier examples (or from the enclosed StackOverflow link) that this error comes from either...

  1. The template is not being loaded (on the quoted example on StackOverflow, nothing seems to call the template...).
  2. .Set(), by design (and not bug!) just passes those values to the middleware, not to the template itself.

(and after my earlier post, I did in fact figure out how to write a very small chunk of middleware and use it to set/reset common variables in my code, so that they get correctly passed to the template. It was easier than I thought!

Comment From: barats

@barats I'd say this is not really an issue any longer. You can see from the earlier examples (or from the enclosed StackOverflow link) that this error comes from either...

  1. The template is not being loaded (on the quoted example on StackOverflow, nothing seems to call the template...).
  2. .Set(), by design (and not bug!) just passes those values to the middleware, not to the template itself.

(and after my earlier post, I did in fact figure out how to write a very small chunk of middleware and use it to set/reset common variables in my code, so that they get correctly passed to the template. It was easier than I thought!

Sadly, you'll have to .Get() EVERY SINGLE variable in controller and pass it to EVERY SINGLE template, isn't it? I don't think this is the correct way to do it.

Comment From: PatchRequest

So if i have a middleware which sets a variable which is used in every template i have to extract it out of the context and pass it to the template in every function explicit? Adding a way to pass directoy from middleware to template would be a way to reduce redundand code