Browse Source

Use funcs in templates instead fields

Vova Tkach 6 years ago
parent
commit
7db59b7399
2 changed files with 75 additions and 9 deletions
  1. 72 0
      engine/fetdata/fetdata.go
  2. 3 9
      modules/module_index.go

+ 72 - 0
engine/fetdata/fetdata.go

@@ -0,0 +1,72 @@
+package fetdata
+
+import (
+	"html/template"
+
+	"golang-fave/engine/wrapper"
+	"golang-fave/utils"
+)
+
+type FERData struct {
+	Wrap    *wrapper.Wrapper
+	DataRow interface{}
+}
+
+func New(wrap *wrapper.Wrapper, drow interface{}) *FERData {
+	fer := &FERData{
+		Wrap:    wrap,
+		DataRow: drow,
+	}
+	return fer.init()
+}
+
+func (this *FERData) init() *FERData {
+	if this.Wrap.CurrModule == "index" {
+		if this.DataRow.(*utils.MySql_page).A_meta_title == "" {
+			this.DataRow.(*utils.MySql_page).A_meta_title = this.DataRow.(*utils.MySql_page).A_name
+		}
+	}
+	return this
+}
+
+func (this *FERData) MetaTitle() string {
+	if this.Wrap.CurrModule == "index" {
+		return this.DataRow.(*utils.MySql_page).A_meta_title
+	}
+	return ""
+}
+
+func (this *FERData) MetaKeywords() string {
+	if this.Wrap.CurrModule == "index" {
+		return this.DataRow.(*utils.MySql_page).A_meta_keywords
+	}
+	return ""
+}
+
+func (this *FERData) MetaDescription() string {
+	if this.Wrap.CurrModule == "index" {
+		return this.DataRow.(*utils.MySql_page).A_meta_description
+	}
+	return ""
+}
+
+func (this *FERData) Name() string {
+	if this.Wrap.CurrModule == "index" {
+		return this.DataRow.(*utils.MySql_page).A_name
+	}
+	return ""
+}
+
+func (this *FERData) Alias() string {
+	if this.Wrap.CurrModule == "index" {
+		return this.DataRow.(*utils.MySql_page).A_alias
+	}
+	return ""
+}
+
+func (this *FERData) Content() template.HTML {
+	if this.Wrap.CurrModule == "index" {
+		return template.HTML(this.DataRow.(*utils.MySql_page).A_content)
+	}
+	return template.HTML("")
+}

+ 3 - 9
modules/module_index.go

@@ -6,13 +6,13 @@ import (
 
 	"fmt"
 	"html"
-	"html/template"
 	"os"
 	"strconv"
 
 	"golang-fave/assets"
 	"golang-fave/consts"
 	"golang-fave/engine/builder"
+	"golang-fave/engine/fetdata"
 	"golang-fave/engine/wrapper"
 	"golang-fave/utils"
 )
@@ -101,14 +101,8 @@ func (this *Modules) RegisterModule_Index() *Module {
 		}
 
 		// Render template
-		wrap.RenderFrontEnd(tmpl_name, consts.TmplDataModIndex{
-			Name:            row.A_name,
-			Alias:           row.A_alias,
-			Content:         template.HTML(row.A_content),
-			MetaTitle:       row.A_meta_title,
-			MetaKeywords:    row.A_meta_keywords,
-			MetaDescription: row.A_meta_description,
-		})
+		tmpl_data := fetdata.New(wrap, row)
+		wrap.RenderFrontEnd(tmpl_name, tmpl_data)
 	}, func(wrap *wrapper.Wrapper) (string, string, string) {
 		content := ""
 		sidebar := ""