Request Context in Go Templates
In Go HTML templates, determining user roles or other request-specific information can be challenging. One may seek to employ conditional fragments like:
{{if isUserAdmin}} <a href"/admin/nuke">Go to the big red nuclear button</a> {{end}}
However, templates lack direct access to request context. Is there an alternative approach to achieving this functionality?
The ideal solution would leverage the request context, eliminating the need for complex pipelines or LogicFuncs. For instance:
<code class="go">type TemplateData struct { Content *Content Context *Context }</code>
This structure embeds both data and context, allowing for selective reuse of shared and query-specific information.
The above is the detailed content of How to Access Request Context in Go HTML Templates?. For more information, please follow other related articles on the PHP Chinese website!