-
Notifications
You must be signed in to change notification settings - Fork 0
/
lv_template_func.go
133 lines (117 loc) · 3.01 KB
/
lv_template_func.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2013 slowfei And The Contributors All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Create on 2013-08-24
// Update on 2015-08-03
// Email slowfei#nnyxing.com
// Home http://www.slowfei.com
// leafveingo web 模板使用函数模块
package LVTemplate
import (
"bytes"
"html/template"
"strings"
)
/**
* 嵌入模板函数
*
* @param tplPath template relative path
* @param data template data
* @return template.HTML
*/
func (l *Template) embedTempate(tplPath string, data interface{}) template.HTML {
tmpl, err := l.Parse(tplPath)
if nil != err {
if l.IsDevel() {
return template.HTML(err.Error())
} else {
return "error code"
}
} else if nil != tmpl {
buf := bytes.NewBuffer(nil)
err = tmpl.Execute(buf, data)
if nil != err {
if l.IsDevel() {
return template.HTML(err.Error())
} else {
return "error code"
}
} else {
return template.HTML(buf.String())
}
}
if l.IsDevel() {
return "Embed Tempate Error"
} else {
return ""
}
}
/**
* 嵌入模版函数,根据模版名查询模版
*
* @param tplName cache template name
* @param data template data
* @return template.HTML
*/
func (l *Template) embedTempateByName(tplName string, data interface{}) template.HTML {
if tmpl := l.goTemplate.Lookup(tplName); nil != tmpl {
bufStr := bytes.NewBufferString("")
tmpl.Execute(bufStr, data)
return template.HTML(bufStr.String())
} else {
return template.HTML("\"" + tplName + "\" name not found template.")
}
}
/**
* string转换成html标签代码
*
* @param str
* @return
*/
func (l *Template) stringToHtml(str string) template.HTML {
return template.HTML(str)
}
/**
* map类型数据的封装
*
* 注意,传递合并的map类型需要是map[string]interface{},否则会出错
* slice值的添加:"array" "value1,value2,value3"
* key value
*
* @mergerMap 需要合并的map
* @strs 根据key value的顺序进行添加
*/
func (l *Template) mapPack(mergerMap map[string]interface{}, strs ...string) map[string]interface{} {
var thisMap map[string]interface{} = nil
// TODO考虑是否需要验证mergerMap的类型
if nil != mergerMap {
thisMap = mergerMap
} else {
thisMap = make(map[string]interface{})
}
count := len(strs)
for i := 0; i < count; i++ {
if i+1 < count {
val := strs[i+1]
if 0 < strings.Index(val, ",") {
slice := strings.Split(val, ",")
thisMap[strs[i]] = slice
} else {
thisMap[strs[i]] = val
}
i++
}
}
return thisMap
}