Browse Source

Templates usage optimisation

Vova Tkach 6 years ago
parent
commit
1aa5ab5111

+ 1 - 1
engine/wrapper/static.go

@@ -107,7 +107,7 @@ func (e *Wrapper) printPage404() {
 	(*e.W).Write(Templates.PageError404)
 }
 
-func (e *Wrapper) PrintTmplPageError(err error) {
+func (e *Wrapper) printTmplPageError(err error) {
 	(*e.W).WriteHeader(http.StatusInternalServerError)
 	(*e.W).Header().Set("Content-Type", "text/html")
 	(*e.W).Write([]byte(fmt.Sprintf(string(Templates.PageTmplError), err.Error())))

+ 43 - 1
engine/wrapper/wrapper.go

@@ -1,6 +1,7 @@
 package wrapper
 
 import (
+	"html/template"
 	"log"
 	"net"
 	"net/http"
@@ -10,6 +11,21 @@ import (
 	"golang-fave/engine/sessions"
 )
 
+type handleRun func(e *Wrapper) bool
+
+type tmplDataSystem struct {
+	PathIcoFav       string
+	PathCssBootstrap string
+	PathJsJquery     string
+	PathJsPopper     string
+	PathJsBootstrap  string
+}
+
+type tmplDataAll struct {
+	System tmplDataSystem
+	Data   interface{}
+}
+
 type Wrapper struct {
 	W            *http.ResponseWriter
 	R            *http.Request
@@ -24,7 +40,15 @@ type Wrapper struct {
 	Debug        bool
 }
 
-type handleRun func(e *Wrapper) bool
+func (e *Wrapper) tmplGetSystemData() tmplDataSystem {
+	return tmplDataSystem{
+		PathIcoFav:       e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/fave.ico",
+		PathCssBootstrap: e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/bootstrap.css",
+		PathJsJquery:     e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/jquery.js",
+		PathJsPopper:     e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/popper.js",
+		PathJsBootstrap:  e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/bootstrap.js",
+	}
+}
 
 func New(w *http.ResponseWriter, r *http.Request, vhost string, port string, wwwdir string, vhosthome string, debug bool) *Wrapper {
 	return &Wrapper{
@@ -143,3 +167,21 @@ func (e *Wrapper) LogError(value string) {
 		"] [" + e.R.URL.Scheme + "://" + e.R.Host + e.R.URL.RequestURI() +
 		"] [" + e.R.Header.Get("User-Agent") + "]")
 }
+
+func (e *Wrapper) TmplFrontEnd(tname string, data interface{}) bool {
+	tmpl, err := template.ParseFiles(
+		e.DirVhostHome+"/template"+"/"+tname+".html",
+		e.DirVhostHome+"/template"+"/header.html",
+		e.DirVhostHome+"/template"+"/sidebar.html",
+		e.DirVhostHome+"/template"+"/footer.html",
+	)
+	if err != nil {
+		e.printTmplPageError(err)
+		return true
+	}
+	tmpl.Execute(*e.W, tmplDataAll{
+		System: e.tmplGetSystemData(),
+		Data:   data,
+	})
+	return true
+}

+ 6 - 34
frontend.go

@@ -1,61 +1,33 @@
 package main
 
 import (
-	"html/template"
-
 	"golang-fave/engine/wrapper"
 )
 
-type TmplMenuItem struct {
-	Name string
-	Link string
+type MenuItem struct {
+	Name   string
+	Link   string
 	Active bool
 }
 
 type TmplData struct {
-	PathSysIcoFav       string
-	PathSysCssBootstrap string
-	PathSysJsJquery     string
-	PathSysJsPopper     string
-	PathSysJsBootstrap  string
-
 	MetaTitle       string
 	MetaKeywords    string
 	MetaDescription string
-	MenuItems       []TmplMenuItem
-	SomeHtml        template.HTML
+	MenuItems       []MenuItem
 }
 
 func handleFrontEnd(e *wrapper.Wrapper) bool {
-	tmpl, err := template.ParseFiles(
-		e.DirVhostHome+"/template"+"/index.html",
-		e.DirVhostHome+"/template"+"/header.html",
-		e.DirVhostHome+"/template"+"/sidebar.html",
-		e.DirVhostHome+"/template"+"/footer.html",
-	)
-	if err != nil {
-		e.PrintTmplPageError(err)
-		return true
-	}
-
-	tmpl.Execute(*e.W, TmplData{
-		PathSysIcoFav:       e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/fave.ico",
-		PathSysCssBootstrap: e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/bootstrap.css",
-		PathSysJsJquery:     e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/jquery.js",
-		PathSysJsPopper:     e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/popper.js",
-		PathSysJsBootstrap:  e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/bootstrap.js",
-
+	return e.TmplFrontEnd("index", TmplData{
 		MetaTitle:       "Meta Title",
 		MetaKeywords:    "Meta Keywords",
 		MetaDescription: "Meta Description",
 
-		MenuItems: []TmplMenuItem{
+		MenuItems: []MenuItem{
 			{Name: "Home", Link: "/", Active: true},
 			{Name: "Item 1", Link: "/#1", Active: false},
 			{Name: "Item 2", Link: "/#2", Active: false},
 			{Name: "Item 3", Link: "/#3", Active: false},
 		},
-		SomeHtml: template.HTML("<div class=\"some-class\">DIV</div>"),
 	})
-	return true
 }

+ 3 - 3
hosts/localhost/template/footer.html

@@ -8,8 +8,8 @@
 		</main>
 		<!-- Optional JavaScript -->
 		<!-- jQuery first, then Popper.js, then Bootstrap JS -->
-		<script src="{{$.PathSysJsJquery}}"></script>
-		<script src="{{$.PathSysJsPopper}}"></script>
-		<script src="{{$.PathSysJsBootstrap}}"></script>
+		<script src="{{$.System.PathJsJquery}}"></script>
+		<script src="{{$.System.PathJsPopper}}"></script>
+		<script src="{{$.System.PathJsBootstrap}}"></script>
 	</body>
 </html>

+ 7 - 7
hosts/localhost/template/header.html

@@ -7,25 +7,25 @@
 		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
 
 		<!-- Bootstrap CSS -->
-		<link rel="stylesheet" href="{{$.PathSysCssBootstrap}}">
+		<link rel="stylesheet" href="{{$.System.PathCssBootstrap}}">
 
-		<title>{{$.MetaTitle}}</title>
+		<title>{{$.Data.MetaTitle}}</title>
 
-		<meta name="keywords" content="{{$.MetaKeywords}}" />
-		<meta name="description" content="{{$.MetaDescription}}" />
+		<meta name="keywords" content="{{$.Data.MetaKeywords}}" />
+		<meta name="description" content="{{$.Data.MetaDescription}}" />
 
-		<link rel="shortcut icon" href="{{$.PathSysIcoFav}}" type="image/x-icon" />
+		<link rel="shortcut icon" href="{{$.System.PathIcoFav}}" type="image/x-icon" />
 	</head>
 	<body class="pt-5">
 		<header>
 			<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
-				<a class="navbar-brand" href="/">{{$.MetaTitle}}</a>
+				<a class="navbar-brand" href="/">{{$.Data.MetaTitle}}</a>
 				<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
 					<span class="navbar-toggler-icon"></span>
 				</button>
 				<div class="collapse navbar-collapse" id="navbarCollapse">
 					<ul class="navbar-nav mr-auto">
-						{{range $.MenuItems}}
+						{{range $.Data.MenuItems}}
 							<li class="nav-item{{if .Active}} active{{end}}">
 								<a class="nav-link" href="{{.Link}}">{{.Name}}</a>
 							</li>