/
std.go
56 lines (44 loc) · 1.3 KB
/
std.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Copyright (c) 2020. The StudyGolang Authors. All rights reserved.
* Use of this source code is governed by a Apache 2.0
* license that can be found in the LICENSE file.
* https://studygolang.com
* Author:polaris polaris@studygolang.com
*/
package render
import (
"html/template"
"io"
"net/http"
"github.com/labstack/echo/v4"
)
type layoutTemplate struct{}
var LayoutTemplate = &layoutTemplate{}
func (l *layoutTemplate) Render(w io.Writer, contentTpl string, data interface{}, ctx echo.Context) error {
layout := "layout.html"
// if data != nil {
// if dataMap, ok := data.(map[string]interface{}); ok {
// if layoutInter, ok := dataMap["layout"]; ok {
// layout = layoutInter.(string)
// }
// }
// }
layoutInter := ctx.Get("layout")
if layoutInter != nil {
layout = layoutInter.(string)
}
tpl, err := template.New(layout).ParseFiles("template/common/"+layout, "template/"+contentTpl)
if err != nil {
return err
}
return tpl.Execute(w, data)
}
// NoNavRender 没有导航的 layout html 输出
func NoNavRender(ctx echo.Context, contentTpl string, data interface{}) error {
// if data == nil {
// data = make(map[string]interface{})
// }
// data["layout"] = "nonav_layout.html"
ctx.Set("layout", "nonav_layout.html")
return ctx.Render(http.StatusOK, contentTpl, data)
}