Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
base.html
{{define "base"}}

<!DOCTYPE html>
<html>
    <head>
        <title>{{template "title" .}}</title>
    </head>
    <body>
        <nav>
            <div> <a href="/">Home</a></div>

            {{if .Loggedin}}
                {{template "nav2" .}}
            {{else}}
                {{template "nav" .}}
            {{end}}
        </nav>

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


login.html

{{template "base" .}}

{{define "title"}} Log In {{end}}

{{define "nav"}}
    <div>
        <a href="/signup">Sign Up</a>
        <a href="/login">Log In</a>
    </div>
{{end}}

{{define "nav2"}}
    <div>
        <a href="/about">About</a>
        <a href="/logout">Logout</a>
    </div>
{{end}}

{{define "body"}}
    {{if .Loggedin}}
        <h1>About section</h1>
    {{else}}
          <-------Login form-------->
    {{end}}
{{end}}


home.html

{{template "base" .}}

{{define "title"}} Home {{end}}

{{define "nav"}}
    <div>
        <a href="/signup">Sign Up</a>
        <a href="/login">Log In</a>
    </div>
{{end}}

{{define "body"}}
    <h1>This is the heading</h1>
    <p>This is a paragraph</p>
{{end}}


What I have tried:

How do I make other HTML files working? In this code, I created a base.html in which I gave the if-statement that if loggedin then show the second navigation bar otherwise show the first one.

I defined both nav and nav2 in login.html. Now when I type localhost:8080/login, it shows me the login form and it works correctly but when I type other URLs like home, about, signup, it does not show me anything.

Although before adding the if-statement in base.html, every URL was showing the page. I added the home.html also as an example to see if the code is correct.
Posted
Updated 30-Jul-21 21:18pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900