Original address
?? Most web apps have the same layout. This layout may contain a header or footer, and may even contain a navigation menu. The standard library of Go provides an easy way to create these basic elements, creating the effect of template pages by reusing different pages.
?? This simple example explains how to accomplish this:
?? Let's create a simple web App with two view, one is the main one is about. These two view all have the same header and footer.
?? The code for the header template is as follows:
{{define ' header '}}<!DOCTYPE HTML><HTML> <Head> <title>{{. Title}}</title> <Linkrel= "stylesheet"href= "//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"> <Linkrel= "stylesheet"href= "//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css"> <styletype= "Text/css">Body{Padding-bottom:70px;}. Content{margin:10px;} </style> </Head> <Body> <navclass= "NavBar Navbar-default"role= "Navigation"> <Divclass= "Navbar-header"> <aclass= "Navbar-brand"href="/">Go App</a> </Div> <Divclass= "Collapse Navbar-collapse navbar-ex1-collapse"> <ulclass= "Nav navbar-nav"> <Li><ahref="/">Main</a></Li> <Li><ahref= "/about">About</a></Li> </ul> </Div> </nav>{{end}}
The code for the footer template is as follows:
{{define ' footer '}}<Pclass= "Navbar-text navbar-fixed-bottom">Go rocks!</P> <Scriptsrc= "//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></Script> </Body></HTML>{{end}}
The code for the main template is as follows:
{{define "main"}}{{template "header".} < div class = "content" > < h2 > Main</ h2 > < div > This is the Main page</ div > </ div Span style= "COLOR: #0000ff" >> {{template "Footer".}} {{end}}
? The code for the About template is as follows:
{{define "about"}}{{template "header".}} < div class = "content" > < h2 > About</ h2 > < div > The About Page</ div > </ div Span style= "COLOR: #0000ff" >> {{template "Footer".}} {{end}}
The server code is as follows:
Package Mainimport ("html/template" "net/http")//Compile templates on StartvarTemplates = template. Must (template. Parsefiles ("header.html","footer.html","main.html","about.html"))//A Page StructureType Pagestruct{Titlestring}//Display the named templateFunc Display (W http. Responsewriter, Tmplstring, the dataInterface{}) {templates. Executetemplate (W, Tmpl, data)}//The handlers.Func MainHandler (w http. Responsewriter, R *http. Request) {display (W,"Main", &page{title:"Home"})}func Abouthandler (w http. Responsewriter, R*http. Request) {display (W," About", &page{title:" About"})}func Main () {http. Handlefunc ("/", MainHandler) http. Handlefunc ("/about", Abouthandler)//Listen on port 8080http. Listenandserve (": 8080", nil)}
Each template page has a define "name"
command to define the name of the template. Main and about pages are passed template "name"
to include headers and footer. "." In the context to name the template. Now, regardless of how the main and about pages are executed, their pages will contain headers and footer.
?? The results of the two pages are as follows:
Create a master page for Go Web App