1

I have two web page on golang and I want to embed this pages codes to {{.content}} variable (defined in templates/main.html) being with dynamic according to the coming requests.

For example if the guest enter the userregister page I want to the {{.content}} variable will be userregister codes otherwise userprofile codes.

templates/userregister.html page codes;

{{ define "userregister" }}
   ...
   {{.specialmessage}}
   ...
{{ end }}

templates/userprofile.html page codes;

{{ define "userprofile" }}
   ...
   {{.specialmessage}}
   ...
{{ end }}

templates/main.html;

{{ define "main" }}
<!DOCTYPE html>
<html lang="tr">
    {{ template "head" . }}
    <body>
        <div class="container-fluid">
            {{ template "header" . }}
            <div class="row">
                <nav class="col-12 col-md-2 p-0">
                    {{ template "leftmenu" . }}
                </nav>
                <div class="container-fluid col-12 col-md-10">


                    {{.content}}


                </div>
            </div>
            {{ template "footer" . }}
        </div>
    </body>
</html>
{{ end }}

The userregister page controller;

func PgUserRegister(c *gin.Context) {
    c.HTML(http.StatusOK,"main", gin.H{
        "pgETitle": "User Register page",
        "specialmessage": "You are on the userregister page.",

        "content": template.ParseFiles("userregister.html"),
    })
}

The userprofile page controller;

func PgUserProfile(c *gin.Context) {
    c.HTML(http.StatusOK,"main", gin.H{
        "pgETitle": "User Profile",
        "specialmessage": "You are on the userprofile page.",

        "content": template.ParseFiles("userprofile.html"),
    })
}

1 Answer 1

2

Parse all templates when starting the router.

router.LoadHTMLFiles("templates/main.html", "templates/userregister.html", "templates/userprofile.html")

Then in your handler add to the gin.H{ ... } expression a boolean variable, e.g. "isLoggedIn", and then in your main template use an if-else action together with the template action.

{{ if .isLoggedIn }}
    {{ template "userprofile" . }}
{{ else }}
    {{ template "userregister" . }}
{{ end }}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, @mkopriva I have a lot of pages for this situation how can I make like it {{ template .pgVariable . }} #### pgVariable is the string variable contains page name?
That's not possible, if you look at the documentation on actions linked above you'll see that the argument must be a literal string containing the name of a template.
@Hakan do you have a lot of templates where you need to switch between these two specific templates? Or do you have a lot of templates where you need to switch between different templates, not just userprofile and userregister?
Actually; I have a lot of template pages but I posted my question being two template pages to simplify the problem. I have about 10 different pages (I am not sure yet at now), Do I have to control the template pages with if statement one by one?
Maybe rethink how you structure those templates, instead of attempting to render everything from main.html, each template would have a template action on top and bottom to render that content that is shared by all of them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.