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.