Browse Source

FrontEnd render func (return and optimize)

Vova Tkach 6 years ago
parent
commit
b252c434a9
4 changed files with 49 additions and 5 deletions
  1. 13 0
      consts/consts.go
  2. 22 0
      engine/wrapper/wrapper.go
  3. 1 1
      hosts/localhost/template/header.html
  4. 13 4
      modules/module_index.go

+ 13 - 0
consts/consts.go

@@ -42,3 +42,16 @@ type TmplData struct {
 	System TmplSystem
 	Data   interface{}
 }
+
+type TmplDataMainMenuItem struct {
+	Name   string
+	Link   string
+	Active bool
+}
+
+type TmplDataModIndex struct {
+	MetaTitle       string
+	MetaKeywords    string
+	MetaDescription string
+	MainMenuItems   []TmplDataMainMenuItem
+}

+ 22 - 0
engine/wrapper/wrapper.go

@@ -4,10 +4,12 @@ import (
 	"database/sql"
 	"errors"
 	"fmt"
+	"html/template"
 	"net/http"
 	"os"
 	"strings"
 
+	"golang-fave/consts"
 	"golang-fave/logger"
 	"golang-fave/utils"
 
@@ -123,3 +125,23 @@ func (this *Wrapper) MsgError(msg string) {
 		`ShowSystemMsgError('Error!', '%s', true);`,
 		strings.Replace(strings.Replace(msg, `'`, `’`, -1), `"`, `”`, -1)))
 }
+
+func (this *Wrapper) RenderFrontEnd(tname string, data interface{}) {
+	tmpl, err := template.ParseFiles(
+		this.DTemplate+string(os.PathSeparator)+tname+".html",
+		this.DTemplate+string(os.PathSeparator)+"header.html",
+		this.DTemplate+string(os.PathSeparator)+"sidebar.html",
+		this.DTemplate+string(os.PathSeparator)+"footer.html",
+	)
+	if err != nil {
+		utils.SystemErrorPageEngine(this.W, err)
+		return
+	}
+	this.W.WriteHeader(http.StatusOK)
+	this.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
+	this.W.Header().Set("Content-Type", "text/html; charset=utf-8")
+	tmpl.Execute(this.W, consts.TmplData{
+		System: utils.GetTmplSystemData(),
+		Data:   data,
+	})
+}

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

@@ -25,7 +25,7 @@
 				</button>
 				<div class="collapse navbar-collapse" id="navbarCollapse">
 					<ul class="navbar-nav mr-auto">
-						{{range $.Data.MenuItems}}
+						{{range $.Data.MainMenuItems}}
 							<li class="nav-item{{if .Active}} active{{end}}">
 								<a class="nav-link" href="{{.Link}}">{{.Name}}</a>
 							</li>

+ 13 - 4
modules/module_index.go

@@ -9,6 +9,7 @@ import (
 	"os"
 	"strconv"
 
+	"golang-fave/consts"
 	"golang-fave/engine/wrapper"
 	"golang-fave/utils"
 )
@@ -20,10 +21,18 @@ func (this *Modules) RegisterModule_Index() *Module {
 		Name:   "Pages",
 	}, func(wrap *wrapper.Wrapper) {
 		// Front-end
-		wrap.W.WriteHeader(http.StatusOK)
-		wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
-		wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
-		wrap.W.Write([]byte(`INDEX FrontEnd func call (` + wrap.CurrModule + `)`))
+		wrap.RenderFrontEnd("index", consts.TmplDataModIndex{
+			MetaTitle:       "Meta Title",
+			MetaKeywords:    "Meta Keywords",
+			MetaDescription: "Meta Description",
+
+			MainMenuItems: []consts.TmplDataMainMenuItem{
+				{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},
+			},
+		})
 	}, func(wrap *wrapper.Wrapper) {
 		// Back-end
 		wrap.W.WriteHeader(http.StatusOK)