本文整理匯總了Golang中html/template.HTML函數的典型用法代碼示例。如果您正苦於以下問題:Golang HTML函數的具體用法?Golang HTML怎麽用?Golang HTML使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了HTML函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TemplateFuncMap
// TemplateFuncMap returns a map of functions that are usable in templates
func TemplateFuncMap() template.FuncMap {
v := config.Raw.View
f := make(template.FuncMap)
f["JS"] = func(s string) template.HTML {
path, err := v.AssetTimePath(s)
if err != nil {
log.Println("JS Error:", err)
return template.HTML("<!-- JS Error: " + s + " -->")
}
return template.HTML(`<script type="text/javascript" src="` + path + `"></script>`)
}
f["CSS"] = func(s string) template.HTML {
path, err := v.AssetTimePath(s)
if err != nil {
log.Println("CSS Error:", err)
return template.HTML("<!-- CSS Error: " + s + " -->")
}
return template.HTML(`<link rel="stylesheet" type="text/css" href="` + path + `" />`)
}
f["LINK"] = func(path, name string) template.HTML {
return template.HTML(`<a href="` + v.PrependBaseURI(path) + `">` + name + `</a>`)
}
return f
}
示例2: itemToBody
func itemToBody(feed *feedparser.Feed, item *feedparser.FeedItem) io.Reader {
tmpl := template.Must(template.New("message").Parse(ctx.Template))
type tmplData struct {
Link string
Title string
Author string
Content template.HTML
}
data := tmplData{
Link: item.Link,
Title: item.Title,
}
if len(item.Content) > 0 {
data.Content = template.HTML(item.Content)
} else {
data.Content = template.HTML(item.Description)
}
var doc bytes.Buffer
tmpl.Execute(&doc, data)
return &doc
}
示例3: ServeHTTP
// Base handler for HTTP requests.
func (*handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if strings.HasPrefix(path, "/css/") {
path = strings.TrimLeft(path, "/css/")
if css, ok := assets.Css[path]; ok {
w.Header().Set("Content-Type", "text/css; charset=utf-8")
w.Write([]byte(css))
} else {
w.WriteHeader(http.StatusNotFound)
}
} else if path == "/favicon.ico" {
} else {
var file []byte
path = strings.TrimLeft(path, "/")
if path == "" {
file = markdown.GetReadme()
} else {
file = markdown.GetFile(path)
}
data := data{
Title: "Knowledge Base",
Index: template.HTML(string(markdown.GetIndex())),
Content: template.HTML(string(file)),
}
render(w, data)
}
}
示例4: GetPanelConfig
func (bbp *BuildBaronPlugin) GetPanelConfig() (*plugin.PanelConfig, error) {
root := plugin.StaticWebRootFromSourceFile()
panelHTML, err := ioutil.ReadFile(root + "/partials/ng_include_task_build_baron.html")
if err != nil {
return nil, fmt.Errorf("Can't load panel html file, %v, %v",
root+"/partials/ng_include_task_build_baron.html", err)
}
includeJS, err := ioutil.ReadFile(root + "/partials/script_task_build_baron_js.html")
if err != nil {
return nil, fmt.Errorf("Can't load panel html file, %v, %v",
root+"/partials/script_task_build_baron_js.html", err)
}
includeCSS, err := ioutil.ReadFile(root +
"/partials/link_task_build_baron_css.html")
if err != nil {
return nil, fmt.Errorf("Can't load panel html file, %v, %v",
root+"/partials/link_task_build_baron_css.html", err)
}
return &plugin.PanelConfig{
StaticRoot: plugin.StaticWebRootFromSourceFile(),
Panels: []plugin.UIPanel{
{
Page: plugin.TaskPage,
Position: plugin.PageRight,
PanelHTML: template.HTML(panelHTML),
Includes: []template.HTML{template.HTML(includeCSS), template.HTML(includeJS)},
},
},
}, nil
}
示例5: render
func (t *Template) render(rctx core.RenderContext) string {
b := &bytes.Buffer{}
// Update functions for current rendering context.
t.tmpl.Funcs(map[string]interface{}{
"slot": func(name, elt string) template.HTML {
s := t.node.Slot(name)
if elt == "" {
return template.HTML(s.Node().Render(rctx))
}
return template.HTML(fmt.Sprintf("<%s id='%s'>%s</%s>", elt, s.ID(), s.Node().Render(rctx), elt))
},
"event": func(name string) template.JS {
return template.JS(fmt.Sprintf("stdweb.events.onClick('%s', '%s', event)", template.JSEscapeString(t.node.ID()), template.JSEscapeString(name)))
},
})
err := t.tmpl.Execute(b, &tplData{
ID: t.node.ID(),
RunID: rctx.RunID(),
UpdateID: rctx.UpdateID(),
Data: t.data,
})
if err == nil {
return b.String()
}
return html.EscapeString(err.Error())
}
示例6: templateFuncMap
func templateFuncMap() template.FuncMap {
return template.FuncMap{
"set": func(renderArgs map[string]interface{}, key string, value interface{}) template.HTML {
renderArgs[key] = value
return template.HTML("")
},
"append": func(renderArgs map[string]interface{}, key string, value interface{}) template.HTML {
if renderArgs[key] == nil {
renderArgs[key] = []interface{}{value}
} else {
renderArgs[key] = append(renderArgs[key].([]interface{}), value)
}
return template.HTML("")
},
// Replaces newlines with <br>
"nl2br": func(text string) template.HTML {
return template.HTML(strings.Replace(template.HTMLEscapeString(text), "\n", "<br>", -1))
},
// Skips sanitation on the parameter. Do not use with dynamic data.
"raw": func(text string) template.HTML {
return template.HTML(text)
},
"datetime": func(date time.Time, format string) string {
return date.Format(format)
},
}
}
示例7: convertMarkdown
func (page *Page) convertMarkdown(lines io.Reader) {
b := new(bytes.Buffer)
b.ReadFrom(lines)
content := string(blackfriday.MarkdownCommon(b.Bytes()))
page.Content = template.HTML(content)
page.Summary = template.HTML(TruncateWordsToWholeSentence(StripHTML(StripShortcodes(content)), summaryLength))
}
示例8: readDescription
func readDescription(attributes []xml.Attr, dec *xml.Decoder) (string, template.HTML) {
var lang string
for _, attribute := range attributes {
if attribute.Name.Local == "lang" {
lang = attribute.Value
}
}
var str string
for {
t, err := dec.Token()
if err != nil {
break
}
switch v := t.(type) {
case xml.CharData:
str += string(v)
case xml.EndElement:
return lang, template.HTML(str)
default:
}
}
// never reached!?!?
return lang, template.HTML(str)
}
示例9: problem
func (pc *AdminProblem) problem() (one model.Problem) {
one.Title = pc.Input.Get("title")
time, err := strconv.Atoi(pc.Input.Get("time"))
if err != nil {
pc.Error("The value 'Time' is neither too short nor too large", 400)
return
}
one.Time = time
memory, err := strconv.Atoi(pc.Input.Get("memory"))
if err != nil {
pc.Error("The value 'Memory' is neither too short nor too large", 400)
return
}
one.Memory = memory
if _, ok := pc.Input["special"]; !ok {
one.Special = 0
} else {
one.Special = 1
}
in := pc.Input.Get("in")
out := pc.Input.Get("out")
one.Description = template.HTML(pc.Input.Get("description"))
one.Input = template.HTML(pc.Input.Get("input"))
one.Output = template.HTML(pc.Input.Get("output"))
one.In = in
one.Out = out
one.Source = pc.Input.Get("source")
one.Hint = template.HTML(pc.Input.Get("hint"))
one.Status = config.StatusReverse
one.ROJ = "ZJGSU"
return one
}
示例10: RenderForm
// render object to form html.
// obj must be a struct pointer.
func RenderForm(obj interface{}) template.HTML {
objT := reflect.TypeOf(obj)
objV := reflect.ValueOf(obj)
if !isStructPtr(objT) {
return template.HTML("")
}
objT = objT.Elem()
objV = objV.Elem()
var raw []string
for i := 0; i < objT.NumField(); i++ {
fieldV := objV.Field(i)
if !fieldV.CanSet() || unKind[fieldV.Kind()] {
continue
}
fieldT := objT.Field(i)
label, name, fType, id, class, ignored := parseFormTag(fieldT)
if ignored {
continue
}
raw = append(raw, renderFormField(label, name, fType, fieldV.Interface(), id, class))
}
return template.HTML(strings.Join(raw, "</br>"))
}
示例11: setState
func (this *orderC) setState(ctx *web.Context,
partnerId int, order *shopping.ValueOrder) {
var descript string
var button string
switch order.Status {
case enum.ORDER_COMPLETED:
descript = `<span style="color:green">訂單已經完成!</span>`
case enum.ORDER_CANCEL:
descript = `<span style="color:red">訂單已經作廢!</span>`
case enum.ORDER_WAIT_CONFIRM:
descript = "確認訂單無誤後,點擊按鈕進行下一步.."
button = `<input class="btn" type="button" id="btn2" value="確認訂單"/>`
case enum.ORDER_WAIT_DELIVERY:
button = `<input class="btn" type="button" id="btn2" value="開始配送"/>`
case enum.ORDER_WAIT_RECEIVE:
button = `<input class="btn" type="button" id="btn2" value="確認收貨"/>`
case enum.ORDER_RECEIVED:
if order.IsPaid == 0 {
descript = `<span style="color:red">訂單尚未付款,如果已經付款,請人工手動付款!</span>`
} else {
descript = "如果已收貨,點擊按鈕完成訂單"
button = `<input class="btn" type="button" id="btn2" value="完成訂單"/>`
}
}
ctx.App.Template().Execute(ctx.Response,
gof.TemplateDataMap{
"button": template.HTML(button),
"descript": template.HTML(descript),
"order_no": order.OrderNo,
}, "views/partner/order/order_setup_setstate.html")
}
示例12: Html
// 在頁麵顯示分頁信息, 內容為 上一頁 當前頁/下一頁 下一頁
func (p *Pagination) Html(number int) template.HTML {
pageCount := int(math.Ceil(float64(p.count) / float64(p.prePage)))
if pageCount <= 1 {
return template.HTML("")
}
linkFlag := "?"
if strings.Index(p.url, "?") > -1 {
linkFlag = "&"
}
html := `<ul class="pager">`
if number > 1 {
html += fmt.Sprintf(`<li class="previous"><a href="%s%sp=%d">← 上一頁</a></li>`, p.url, linkFlag, number-1)
}
html += fmt.Sprintf(`<li class="number">%d/%d</li>`, number, pageCount)
if number < pageCount {
html += fmt.Sprintf(`<li class="next"><a href="%s%sp=%d">下一頁 →</a></li>`, p.url, linkFlag, number+1)
}
return template.HTML(html)
}
示例13: PreviewHTML
// post's preview html,
// use "<!--more-->" to separate, return first part
func (p *Post) PreviewHTML() template.HTML {
bytes := bytes.Split(p.Raw, []byte("<!--more-->"))[0]
if p.rawType == "markdown" {
return template.HTML(helper.Markdown(bytes))
}
return template.HTML(bytes)
}
示例14: renderIndex
func renderIndex(context *common.AppContext, userWrapper, uploadedFlag interface{}) []byte {
// Generate the markup for the index template.
if userWrapper == nil {
context.Log.Print("Attempting to render the login template")
markup := executeTemplate(context, "login", nil)
if markup == nil {
return nil
}
params := map[string]interface{}{"LayoutContent": template.HTML(string(markup))}
return executeTemplate(context, "head", params)
} else if uploadedFlag == nil {
context.Log.Print("Attempting to render the upload template")
markup := executeTemplate(context, "upload", nil)
if markup == nil {
return nil
}
params := map[string]interface{}{"LayoutContent": template.HTML(string(markup))}
return executeTemplate(context, "head", params)
}
context.Log.Print("Attempting to render the search template")
markup := executeTemplate(context, "search", nil)
if markup == nil {
return nil
}
params := map[string]interface{}{"LayoutContent": template.HTML(string(markup))}
return executeTemplate(context, "head", params)
}
示例15: StatusAsHTML
// StatusAsHTML returns an HTML version of our status.
// It works best if there is data in the cache.
func (st *SrvKeyspaceCacheStatus) StatusAsHTML() template.HTML {
if st.Value == nil {
return template.HTML("No Data")
}
result := "<b>Partitions:</b><br>"
for tabletType, keyspacePartition := range st.Value.Partitions {
result += " <b>" + string(tabletType) + "</b>"
for _, shard := range keyspacePartition.ShardReferences {
result += " " + shard.Name
}
result += "<br>"
}
if st.Value.ShardingColumnName != "" {
result += "<b>ShardingColumnName:</b> " + st.Value.ShardingColumnName + "<br>"
result += "<b>ShardingColumnType:</b> " + string(st.Value.ShardingColumnType) + "<br>"
}
if len(st.Value.ServedFrom) > 0 {
result += "<b>ServedFrom:</b><br>"
for tabletType, keyspace := range st.Value.ServedFrom {
result += " <b>" + string(tabletType) + "</b> " + keyspace + "<br>"
}
}
return template.HTML(result)
}