From aac0e2b73d5511fdd0d50de2a671a339cfdc9522 Mon Sep 17 00:00:00 2001 From: Chen Xiongjie Date: Mon, 9 Aug 2021 16:48:50 +0200 Subject: [PATCH] updated: [sitekit]use go:embed in template entry(main.html) --- sitekit/sitekit.go | 10 +++++++--- sitekit/templates/main.html | 27 +++++++++++++++++++++++++-- sitekit/templates/micros.html | 24 ------------------------ 3 files changed, 32 insertions(+), 29 deletions(-) delete mode 100644 sitekit/templates/micros.html diff --git a/sitekit/sitekit.go b/sitekit/sitekit.go index d79982f4..3d1fc505 100644 --- a/sitekit/sitekit.go +++ b/sitekit/sitekit.go @@ -2,11 +2,11 @@ package sitekit import ( "context" + _ "embed" "errors" "fmt" "io" "net/http" - "os" "strconv" "strings" @@ -21,6 +21,9 @@ import ( "github.com/gorilla/mux" ) +//go:embed templates/main.html +var mainTemplate []byte + var siteSettings = map[string]SiteSettings{} var siteIdentifiers = []string{} @@ -249,8 +252,9 @@ func Output(w io.Writer, variables map[string]interface{}, ctx context.Context) if log.GetContextInfo(ctx).CanDebug() { variables["debug"] = true } - gopath := os.Getenv("GOPATH") - tpl := pongo2.Must(pongo2.FromCache(gopath + "/src/github.com/digimakergo/digimaker/sitekit/templates/main.html")) //todo: use configuration + + tpl := pongo2.Must(pongo2.FromBytes(mainTemplate)) + //todo: support import in template - hard in go embed? info := RequestInfo{Context: ctx, Site: variables["site"].(string), SitePath: variables["sitepath"].(string)} diff --git a/sitekit/templates/main.html b/sitekit/templates/main.html index 0309700e..4ed79cbd 100644 --- a/sitekit/templates/main.html +++ b/sitekit/templates/main.html @@ -1,5 +1,3 @@ -{%import "micros.html" output_content,output_field,output%} - {% comment %} Available varilables: @@ -16,6 +14,31 @@ - parameter given in handler/controller {% endcomment %} +{% macro output_content( content, viewmode) export%} + {%with dm.tpl_content(content, viewmode) as path%} + {% if path=="" %} + {OVERRIDE NOT FOUND, PLEASE OVERRIDE. {{content.ID}},{{content.Name}},{{viewmode}}} + {%else%} + {% if debug %}{%endif%} + {%include path|dm_abs_path %} + {% endif %} + {%endwith%} +{% endmacro %} + +{% macro output_field( field ) export%} + {%with "fieldtype/"|add:field.Type()|add:".html"|dm_abs_path:"github.com/digimakergo/digimaker/sitekit" as path%} + {%include path with field=field%} + {%endwith%} +{% endmacro %} + +{#include path from override #} +{% macro output( path, match_data ) export%} + {#todo: match it#} + {%with "../default/"|add:path|add:".html" as template%} + {%include template%} + {%endwith%} +{% endmacro %} + {%if error != nil %} {{error}} {%comment%} todo: use include {%endcomment%} {%else%} diff --git a/sitekit/templates/micros.html b/sitekit/templates/micros.html deleted file mode 100644 index 53d3ad36..00000000 --- a/sitekit/templates/micros.html +++ /dev/null @@ -1,24 +0,0 @@ -{% macro output_content( content, viewmode) export%} - {%with dm.tpl_content(content, viewmode) as path%} - {% if path=="" %} - {OVERRIDE NOT FOUND, PLEASE OVERRIDE. {{content.ID}},{{content.Name}},{{viewmode}}} - {%else%} - {% if debug %}{%endif%} - {%include path|dm_abs_path %} - {% endif %} - {%endwith%} -{% endmacro %} - -{% macro output_field( field ) export%} - {%with "fieldtype/"|add:field.Type()|add:".html"|dm_abs_path:"github.com/digimakergo/digimaker/sitekit" as path%} - {%include path with field=field%} - {%endwith%} -{% endmacro %} - -{#include path from override #} -{% macro output( path, match_data ) export%} - {#todo: match it#} - {%with "../default/"|add:path|add:".html" as template%} - {%include template%} - {%endwith%} -{% endmacro %}