當前位置: 首頁>>代碼示例>>Golang>>正文


Golang template.HTMLEscapeString函數代碼示例

本文整理匯總了Golang中text/template.HTMLEscapeString函數的典型用法代碼示例。如果您正苦於以下問題:Golang HTMLEscapeString函數的具體用法?Golang HTMLEscapeString怎麽用?Golang HTMLEscapeString使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了HTMLEscapeString函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Radios

func (w *RadioWidget) Radios(attrs []string, checkedValue string) []template.HTML {
	id, _ := w.Attrs().Get("id")
	radios := make([]template.HTML, 0, len(w.choices))
	for i, choice := range w.choices {
		wAttrs := w.Attrs().Clone()
		wAttrs.Set("id", fmt.Sprintf("%v_%v", id, i))
		wAttrs.FromSlice(attrs)

		value := tTemplate.HTMLEscapeString(choice[0])
		label := tTemplate.HTMLEscapeString(choice[1])

		checked := ""
		if value == checkedValue {
			checked = ` checked="checked"`
		}

		radio := fmt.Sprintf(
			`<input%v value="%v"%v /> %v`,
			wAttrs.String(),
			value,
			checked,
			label)
		radios = append(radios, template.HTML(radio))
	}
	return radios
}
開發者ID:RangelReale,項目名稱:gforms,代碼行數:26,代碼來源:widgets.go

示例2: login

func login(w http.ResponseWriter, r *http.Request) {
	fmt.Println("method:", r.Method) //get request method
	if r.Method == "GET" {
		crutime := time.Now().Unix()
		h := md5.New()
		io.WriteString(h, strconv.FormatInt(crutime, 10))
		token := fmt.Sprintf("%x", h.Sum(nil))

		t, _ := template.ParseFiles("login.gtpl")
		t.Execute(w, token)
	} else {
		// log in request
		r.ParseForm()
		token := r.Form.Get("token")
		if token != "" {
			fmt.Println("Token: ", token)
			// check token validity
		} else {
			// give error if no token
		}
		fmt.Println("username length:", len(r.Form["username"][0]))
		fmt.Println("username:", template.HTMLEscapeString(r.Form.Get("username"))) // print in server side
		fmt.Println("password:", template.HTMLEscapeString(r.Form.Get("password")))
		template.HTMLEscape(w, []byte(r.Form.Get("username"))) // respond to client
	}
}
開發者ID:espang,項目名稱:examples,代碼行數:26,代碼來源:server.go

示例3: login

func login(w http.ResponseWriter, r *http.Request) {
	fmt.Println("method:", r.Method) //獲取請求的方法
	if r.Method == "GET" {
		crutime := time.Now().Unix()
		h := md5.New()
		io.WriteString(h, strconv.FormatInt(crutime, 10))
		token := fmt.Sprintf("%x", h.Sum(nil))

		t, _ := template.ParseFiles("login.gtpl")
		t.Execute(w, token)
	} else {
		//請求的是登陸數據,那麽執行登陸的邏輯判斷
		r.ParseForm()
		token := r.Form.Get("token")
		fmt.Println(token)
		if token != "" {

			fmt.Println("***************************")
			//驗證token的合法性
		} else {
			fmt.Println("============================")
			//不存在token報錯
		}
		fmt.Println("username length:", len(r.Form["username"][0]))
		fmt.Println("username:", template.HTMLEscapeString(r.Form.Get("username"))) //輸出到服務器端
		fmt.Println("password:", template.HTMLEscapeString(r.Form.Get("password")))
		template.HTMLEscape(w, []byte(r.Form.Get("username"))) //輸出到客戶端
	}
}
開發者ID:pokerG,項目名稱:GoBeginner,代碼行數:29,代碼來源:upload.go

示例4: Format

func (a *FragmentFormatterEx) Format(f *highlight.Fragment, orderedTermLocations highlight.TermLocations) string {
	rv := ""
	curr := f.Start
	for _, termLocation := range orderedTermLocations {
		if termLocation == nil {
			continue
		}
		if termLocation.Start < curr {
			continue
		}
		if termLocation.End > f.End {
			break
		}
		// add the stuff before this location
		rv += tt.HTMLEscapeString(string(f.Orig[curr:termLocation.Start]))
		// add the color
		rv += a.before
		// add the term itself
		rv += tt.HTMLEscapeString(string(f.Orig[termLocation.Start:termLocation.End]))
		// reset the color
		rv += a.after
		// update current
		curr = termLocation.End
	}
	// add any remaining text after the last token
	rv += tt.HTMLEscapeString(string(f.Orig[curr:f.End]))

	return rv
}
開發者ID:oblank,項目名稱:knowledgedb,代碼行數:29,代碼來源:fragmentformatterex.go

示例5: handleSign

func handleSign(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		serve404(w)
		return
	}
	c := appengine.NewContext(r)
	u := user.Current(c)
	userName := ""
	if u != nil { //a google user
		userName = u.String()
	} else { //not a google user
		//is it a local user?
		cookie, err := r.Cookie("email")
		if err == nil {
			userName = cookie.Value
		} else { //no logged in yet

			badRequest(w, "Only login user can post messages.")
			return
		}
	}
	if err := r.ParseForm(); err != nil {
		serveError(c, w, err)
		return
	}
	tagsString := r.FormValue("tags")
	m := &Message{
		ID:      0,
		Title:   template.HTMLEscapeString(r.FormValue("title")),
		Author:  template.HTMLEscapeString(r.FormValue("author")),
		Content: []byte(template.HTMLEscapeString(r.FormValue("content"))),
		Tags:    strings.Split(template.HTMLEscapeString(tagsString), ","),
		Date:    time.Now(),
		Views:   0,
		Good:    0,
		Bad:     0,
	}
	if badTitle(m.Title) || badAuthor(m.Author) || badContent(string(m.Content)) || badTag(tagsString) {
		badRequest(w, "Input too long")
		return
	}

	processMsgContent(m)
	//TODO: build References and Referedby list
	if u := user.Current(c); u != nil {
		m.Author = userName
		//TODO: hook this message under user's msglist
	}
	k, err := datastore.Put(c, datastore.NewIncompleteKey(c, "aMessage", nil), m)
	if err != nil {
		serveError(c, w, err)
		return
	}
	putMsgTags(r, k.IntID(), m.Tags)
	setCount(w, r)
	http.Redirect(w, r, "/", http.StatusFound)
}
開發者ID:DeanSinaean,項目名稱:csnuts,代碼行數:57,代碼來源:handleSign.go

示例6: Compute

func Compute(uc *understand.Circuit) *Circuit {
	c := &Circuit{Name: uc.Name()}

	// Peers
	var z float64 // Total weight
	var i int
	inv := make(map[*understand.Peer]int)
	for _, p := range uc.Peer {
		inv[p] = i
		// weight := float64(len(p.Valve))
		z += 1
		c.Peer = append(c.Peer,
			&Peer{
				ID:     fmt.Sprintf("peer-%s", p.Name),
				Name:   template.HTMLEscapeString(fmt.Sprintf("%v", p.Name)),
				Design: template.HTMLEscapeString(fmt.Sprintf("%v", p.Design)),
				Weight: 1, //weight,
			},
		)
		i++
	}
	var u float64
	const MaxRadius = 0.9
	for _, p := range c.Peer {
		p.Angle = 2 * math.Pi * (u + p.Weight/2) / z
		p.Anchor = CirclePointOfAngle(p.Angle)
		p.Radius = MaxRadius * p.Weight / z
		u += p.Weight
	}

	// Matchings
	for _, p := range uc.Peer { // From
		pp := c.Peer[inv[p]]
		for _, v := range p.Valve { // To
			qq := c.Peer[inv[v.Matching.Of]]
			x := pp.Anchor
			y := qq.Anchor
			c.Match = append(c.Match,
				&Match{
					ID:          fmt.Sprintf("match-%s-%s", pp.Name, v.Name),
					FromAnchor:  x,
					ToAnchor:    y,
					FromTangent: Scalar(0.5, x),
					ToTangent:   Scalar(0.5, y),
					Valve:       v.Name,
				},
			)
		}
	}
	return c
}
開發者ID:dasmithii,項目名稱:escher,代碼行數:51,代碼來源:draw.go

示例7: directiveInsertWordBreaks

func directiveInsertWordBreaks(value data.Value, args []data.Value) data.Value {
	var (
		input    = template.HTMLEscapeString(value.String())
		maxChars = int(args[0].(data.Int))
		chars    = 0
		output   *bytes.Buffer // create the buffer lazily
	)
	for i, ch := range input {
		switch {
		case ch == ' ':
			chars = 0
		case chars >= maxChars:
			if output == nil {
				output = bytes.NewBufferString(input[:i])
			}
			output.WriteString("<wbr>")
			chars = 1
		default:
			chars++
		}
		if output != nil {
			output.WriteRune(ch)
		}
	}
	if output == nil {
		return value
	}
	return data.String(output.String())
}
開發者ID:voidException,項目名稱:soy,代碼行數:29,代碼來源:directives.go

示例8: Options

func (w *SelectWidget) Options(selValues ...string) []string {
	options := make([]string, 0, len(w.choices))
	for _, choice := range w.choices {
		value := tTemplate.HTMLEscapeString(choice[0])
		label := tTemplate.HTMLEscapeString(choice[1])
		attrs := ""
		for _, selValue := range selValues {
			if value == selValue {
				attrs = ` selected="selected"`
			}
		}
		option := fmt.Sprintf(`<option value="%v"%v>%v</option>`, value, attrs, label)
		options = append(options, option)
	}
	return options
}
開發者ID:RangelReale,項目名稱:gforms,代碼行數:16,代碼來源:widgets.go

示例9: parse_input

func (disp_handler DispHandler) parse_input(r *http.Request) (locate_id uint32, err error) {

	//先解析獲取數據
	err = r.ParseForm()

	if err != nil {
		return
	}

	locate_id = 0
	var tmp uint64 = 0

	str_locate_id, ok := r.Form["locate_id"]

	if ok == false {
		err = ErrorInputParameter
		return
	} else {
		tmp, err = strconv.ParseUint(template.HTMLEscapeString(str_locate_id[0]), 10, 32)
		if err != nil || tmp == 0 {
			err = ErrorInputParameter
			return
		} else {
			locate_id = uint32(tmp)
		}
	}

	err = nil
	return
}
開發者ID:chouqiu,項目名稱:weishenghuo,代碼行數:30,代碼來源:disp.go

示例10: code

func code(file string, arg []interface{}) (string, error) {
	text := contents(file)
	var command string
	switch len(arg) {
	case 0:
		// whole file.
		command = fmt.Sprintf("code %q", file)
	case 1:
		// one line, specified by line or regexp.
		command = fmt.Sprintf("code %q %s", file, format(arg[0]))
		text = oneLine(file, text, arg[0])
	case 2:
		// multiple lines, specified by line or regexp.
		command = fmt.Sprintf("code %q %s %s", file, format(arg[0]), format(arg[1]))
		text = multipleLines(file, text, arg[0], arg[1])
	default:
		return "", fmt.Errorf("incorrect code invocation: code %q %v", file, arg)
	}
	// Replace tabs by spaces, which work better in HTML.
	text = strings.Replace(text, "\t", "    ", -1)
	// Escape the program text for HTML.
	if *html {
		text = template.HTMLEscapeString(text)
	}
	// Include the command as a comment.
	if *html {
		text = fmt.Sprintf("<!--{{%s}}\n-->%s", command, text)
	} else {
		text = fmt.Sprintf("// %s\n%s", command, text)
	}
	return text, nil
}
開發者ID:khasteh,項目名稱:chat,代碼行數:32,代碼來源:makeslide.go

示例11: exampleIdFn

func exampleIdFn(v interface{}, example *doc.Example) string {
	buf := make([]byte, 0, 64)
	buf = append(buf, "_example"...)

	switch v := v.(type) {
	case *doc.Type:
		buf = append(buf, '_')
		buf = append(buf, v.Name...)
	case *doc.Func:
		buf = append(buf, '_')
		if v.Recv != "" {
			if v.Recv[0] == '*' {
				buf = append(buf, v.Recv[1:]...)
			} else {
				buf = append(buf, v.Recv...)
			}
			buf = append(buf, '_')
		}
		buf = append(buf, v.Name...)
	}
	if example.Name != "" {
		buf = append(buf, '-')
		buf = append(buf, example.Name...)
	}
	return template.HTMLEscapeString(string(buf))
}
開發者ID:jeffallen,項目名稱:gopkgdoc,代碼行數:26,代碼來源:template.go

示例12: breadcrumbsFn

func breadcrumbsFn(pdoc *doc.Package) string {
	if !strings.HasPrefix(pdoc.ImportPath, pdoc.ProjectRoot) {
		return ""
	}
	var buf bytes.Buffer
	i := 0
	j := len(pdoc.ProjectRoot)
	if j == 0 {
		buf.WriteString("<a href=\"/-/go\" title=\"Standard Packages\">☆</a> ")
		j = strings.IndexRune(pdoc.ImportPath, '/')
		if j < 0 {
			j = len(pdoc.ImportPath)
		}
	}
	for {
		buf.WriteString(`<a href="/`)
		buf.WriteString(urlFn(pdoc.ImportPath[:j]))
		buf.WriteString(`">`)
		buf.WriteString(template.HTMLEscapeString(pdoc.ImportPath[i:j]))
		buf.WriteString("</a>")
		i = j + 1
		if i >= len(pdoc.ImportPath) {
			break
		}
		buf.WriteByte('/')
		j = strings.IndexRune(pdoc.ImportPath[i:], '/')
		if j < 0 {
			j = len(pdoc.ImportPath)
		} else {
			j += i
		}
	}
	return buf.String()
}
開發者ID:jeffallen,項目名稱:gopkgdoc,代碼行數:34,代碼來源:template.go

示例13: code

func code(file string, arg ...interface{}) (string, error) {
	text := contents(file)
	var command string
	switch len(arg) {
	case 0:
		// text is already whole file.
		command = fmt.Sprintf("code %q", file)
	case 1:
		command = fmt.Sprintf("code %q %s", file, format(arg[0]))
		text = oneLine(file, text, arg[0])
	case 2:
		command = fmt.Sprintf("code %q %s %s", file, format(arg[0]), format(arg[1]))
		text = multipleLines(file, text, arg[0], arg[1])
	default:
		return "", fmt.Errorf("incorrect code invocation: code %q %q", file, arg)
	}
	// Trim spaces from output.
	text = strings.Trim(text, "\n")
	// Replace tabs by spaces, which work better in HTML.
	text = strings.Replace(text, "\t", "    ", -1)
	// Escape the program text for HTML.
	text = template.HTMLEscapeString(text)
	// Include the command as a comment.
	text = fmt.Sprintf("<pre><!--{{%s}}\n-->%s</pre>", command, text)
	return text, nil
}
開發者ID:joninvski,項目名稱:go,代碼行數:26,代碼來源:tmpltohtml.go

示例14: TestEscapge

func TestEscapge() {
	str := template.HTMLEscapeString("<br>hello</br>")
	fmt.Println(str)

	jsStr := template.JSEscapeString("<script>alert('123')</script>")
	fmt.Println(jsStr)
}
開發者ID:yunkaiyueming,項目名稱:go_code,代碼行數:7,代碼來源:template.go

示例15: importPathFn

// importPathFn formats an import with zero width space characters to allow for breaks.
func importPathFn(path string) string {
	path = template.HTMLEscapeString(path)
	if len(path) > 45 {
		// Allow long import paths to break following "/"
		path = strings.Replace(path, "/", "/&#8203;", -1)
	}
	return path
}
開發者ID:jeffallen,項目名稱:gopkgdoc,代碼行數:9,代碼來源:template.go


注:本文中的text/template.HTMLEscapeString函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。