Golang Templates how do i dynamically update the template based on user logged in status

  Kiến thức lập trình

I have an issue where if the user is logged in or logged out the sidebar html that is nested should be different. Now i believe the normal way to solve this is to pass down a .LoggedIn value (this means i would have to pass this on every request where the side bar is present, every page).

I am using HTMX, and i believe we are supposed to do this check on the server side and then just sent the correct HTML. If i check if the user is logged in on the server i then pass down my base.html with all the nest templates but then need to tell the template to use the correct sidebar, how do i do this?

{{ define "title" }}Page Title{{ end }} 
{{ define "sidebar" }} 
    {{ if .LoggedIn }} 
        {{ template "sidebar_logged_in" . }} 
    {{ else }}
        {{ template "sidebar_logged_out" . }} 
    {{ end }} 
{{ end }} 
{{ define "content" }} 
    <h1>Page Content</h1> 
    <p>This is the main content of the page.</p> 
{{ end }} 
{{ template "base.html" . }}

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT