Browse Source

Cache blocks progress

Vova Tkach 5 years ago
parent
commit
9525d5eff6

+ 13 - 1
assets/template/cached_block_4_html_file.go

@@ -1,3 +1,15 @@
 package template
 
-var VarCachedBlock_4HtmlFile = []byte(``)
+var VarCachedBlock_4HtmlFile = []byte(`{{if $.Data.Shop.Category}}
+	{{range $.Data.Shop.Categories $.Data.Shop.Category.Id 1}}
+		<li class="{{if and $.Data.Shop.Category (eq $.Data.Shop.Category.Id .Id)}}active{{end}}">
+			<a href="{{.Permalink}}">{{.Name}}</a>
+		</li>
+	{{end}}
+{{else}}
+	{{range $.Data.Shop.Categories 0 1}}
+		<li class="{{if and $.Data.Shop.Category (eq $.Data.Shop.Category.Id .Id)}}active{{end}}">
+			<a href="{{.Permalink}}">{{.Name}}</a>
+		</li>
+	{{end}}
+{{end}}`)

+ 5 - 1
assets/template/cached_block_5_html_file.go

@@ -1,3 +1,7 @@
 package template
 
-var VarCachedBlock_5HtmlFile = []byte(``)
+var VarCachedBlock_5HtmlFile = []byte(`{{range $.Data.Blog.Categories 0 0}}
+	<li class="{{if and $.Data.Blog.Category (eq $.Data.Blog.Category.Id .Id)}}active{{end}}">
+		<a href="{{.Permalink}}">{{.Name}}</a>
+	</li>
+{{end}}`)

+ 1 - 13
assets/template/sidebar_left_html_file.go

@@ -4,19 +4,7 @@ var VarSidebarLeftHtmlFile = []byte(`<div class="card mb-4">
 	<h5 class="card-header">Shop categories</h5>
 	<div class="card-body">
 		<ul class="m-0 p-0 pl-4">
-			{{if $.Data.Shop.Category}}
-				{{range $.Data.Shop.Categories $.Data.Shop.Category.Id 1}}
-					<li class="{{if and $.Data.Shop.Category (eq $.Data.Shop.Category.Id .Id)}}active{{end}}">
-						<a href="{{.Permalink}}">{{.Name}}</a>
-					</li>
-				{{end}}
-			{{else}}
-				{{range $.Data.Shop.Categories 0 1}}
-					<li class="{{if and $.Data.Shop.Category (eq $.Data.Shop.Category.Id .Id)}}active{{end}}">
-						<a href="{{.Permalink}}">{{.Name}}</a>
-					</li>
-				{{end}}
-			{{end}}
+			{{$.Data.CachedBlock4}}
 		</ul>
 	</div>
 </div>

+ 1 - 5
assets/template/sidebar_right_html_file.go

@@ -4,11 +4,7 @@ var VarSidebarRightHtmlFile = []byte(`<div class="card mb-4">
 	<h5 class="card-header">Blog categories</h5>
 	<div class="card-body">
 		<ul class="m-0 p-0 pl-4">
-			{{range $.Data.Blog.Categories 0 0}}
-				<li class="{{if and $.Data.Blog.Category (eq $.Data.Blog.Category.Id .Id)}}active{{end}}">
-					<a href="{{.Permalink}}">{{.Name}}</a>
-				</li>
-			{{end}}
+			{{$.Data.CachedBlock5}}
 		</ul>
 	</div>
 </div>

+ 58 - 10
engine/fetdata/fetdata.go

@@ -1,9 +1,13 @@
 package fetdata
 
 import (
+	"bytes"
 	"html"
+	"html/template"
+	"os"
 	"time"
 
+	"golang-fave/consts"
 	"golang-fave/engine/wrapper"
 	"golang-fave/utils"
 )
@@ -131,22 +135,66 @@ func (this *FERData) EscapeString(str string) string {
 	return html.EscapeString(str)
 }
 
-func (this *FERData) CachedBlock1() string {
-	return ""
+func (this *FERData) cachedBlock(block string) template.HTML {
+	tmplFuncs := template.FuncMap{
+		"plus": func(a, b int) int {
+			return a + b
+		},
+		"minus": func(a, b int) int {
+			return a - b
+		},
+		"multiply": func(a, b int) int {
+			return a * b
+		},
+		"divide": func(a, b int) int {
+			return a / b
+		},
+		"repeat": func(a string, n int) template.HTML {
+			out := ""
+			for i := 1; i <= n; i++ {
+				out += a
+			}
+			return template.HTML(out)
+		},
+	}
+	tmpl, err := template.New(block + ".html").Funcs(tmplFuncs).ParseFiles(
+		this.wrap.DTemplate + string(os.PathSeparator) + block + ".html",
+	)
+	if err != nil {
+		return template.HTML(err.Error())
+	}
+	var tpl bytes.Buffer
+	err = tmpl.Execute(&tpl, consts.TmplData{
+		System: utils.GetTmplSystemData("", ""),
+		Data:   this,
+	})
+	if err != nil {
+		return template.HTML(err.Error())
+	}
+	return template.HTML(string(tpl.Bytes()))
+}
+
+func (this *FERData) CachedBlock1() template.HTML {
+	return this.cachedBlock("cached-block-1")
+	// return template.HTML("")
 }
 
-func (this *FERData) CachedBlock2() string {
-	return ""
+func (this *FERData) CachedBlock2() template.HTML {
+	return this.cachedBlock("cached-block-2")
+	// return template.HTML("")
 }
 
-func (this *FERData) CachedBlock3() string {
-	return ""
+func (this *FERData) CachedBlock3() template.HTML {
+	return this.cachedBlock("cached-block-3")
+	// return template.HTML("")
 }
 
-func (this *FERData) CachedBlock4() string {
-	return ""
+func (this *FERData) CachedBlock4() template.HTML {
+	return this.cachedBlock("cached-block-4")
+	// return template.HTML("")
 }
 
-func (this *FERData) CachedBlock5() string {
-	return ""
+func (this *FERData) CachedBlock5() template.HTML {
+	return this.cachedBlock("cached-block-5")
+	// return template.HTML("")
 }

+ 13 - 0
hosts/localhost/template/cached-block-4.html

@@ -0,0 +1,13 @@
+{{if $.Data.Shop.Category}}
+	{{range $.Data.Shop.Categories $.Data.Shop.Category.Id 1}}
+		<li class="{{if and $.Data.Shop.Category (eq $.Data.Shop.Category.Id .Id)}}active{{end}}">
+			<a href="{{.Permalink}}">{{.Name}}</a>
+		</li>
+	{{end}}
+{{else}}
+	{{range $.Data.Shop.Categories 0 1}}
+		<li class="{{if and $.Data.Shop.Category (eq $.Data.Shop.Category.Id .Id)}}active{{end}}">
+			<a href="{{.Permalink}}">{{.Name}}</a>
+		</li>
+	{{end}}
+{{end}}

+ 5 - 0
hosts/localhost/template/cached-block-5.html

@@ -0,0 +1,5 @@
+{{range $.Data.Blog.Categories 0 0}}
+	<li class="{{if and $.Data.Blog.Category (eq $.Data.Blog.Category.Id .Id)}}active{{end}}">
+		<a href="{{.Permalink}}">{{.Name}}</a>
+	</li>
+{{end}}

+ 1 - 13
hosts/localhost/template/sidebar-left.html

@@ -2,19 +2,7 @@
 	<h5 class="card-header">Shop categories</h5>
 	<div class="card-body">
 		<ul class="m-0 p-0 pl-4">
-			{{if $.Data.Shop.Category}}
-				{{range $.Data.Shop.Categories $.Data.Shop.Category.Id 1}}
-					<li class="{{if and $.Data.Shop.Category (eq $.Data.Shop.Category.Id .Id)}}active{{end}}">
-						<a href="{{.Permalink}}">{{.Name}}</a>
-					</li>
-				{{end}}
-			{{else}}
-				{{range $.Data.Shop.Categories 0 1}}
-					<li class="{{if and $.Data.Shop.Category (eq $.Data.Shop.Category.Id .Id)}}active{{end}}">
-						<a href="{{.Permalink}}">{{.Name}}</a>
-					</li>
-				{{end}}
-			{{end}}
+			{{$.Data.CachedBlock4}}
 		</ul>
 	</div>
 </div>

+ 1 - 5
hosts/localhost/template/sidebar-right.html

@@ -2,11 +2,7 @@
 	<h5 class="card-header">Blog categories</h5>
 	<div class="card-body">
 		<ul class="m-0 p-0 pl-4">
-			{{range $.Data.Blog.Categories 0 0}}
-				<li class="{{if and $.Data.Blog.Category (eq $.Data.Blog.Category.Id .Id)}}active{{end}}">
-					<a href="{{.Permalink}}">{{.Name}}</a>
-				</li>
-			{{end}}
+			{{$.Data.CachedBlock5}}
 		</ul>
 	</div>
 </div>