请教text template中的变量处理问题

karl_zhao · 2018-09-04 14:25:38 · 1080 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2018-09-04 14:25:38 的主题,其中的信息可能已经有所发展或是发生改变。

请教一下: 在下面的一个模板string里面,从一个结构体里面传入一个name字符串变量。 tpl_str := "hello, {{.name}}" tpl_str += " {{.name}} is a machine。"

我想让传入的name变量在第一行所有字母都是大写的,怎么来操作呢?

先谢谢了!


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1080 次点击  
加入收藏 微博
5 回复  |  直到 2018-09-04 17:16:37
jarlyyn
jarlyyn · #1 · 7年之前

正常做法 ,你需要写一个filter。

https://golang.org/pkg/text/template/#FuncMap

temlate文档中本身也有

karl_zhao
karl_zhao · #2 · 7年之前
jarlyynjarlyyn #1 回复

正常做法 ,你需要写一个filter。 https://golang.org/pkg/text/template/#FuncMap temlate文档中本身也有

我是新手,能否详细指点一下??

jarlyyn
jarlyyn · #3 · 7年之前

我给的链接下面就是 example啊,这个也需要我贴么……

package main

import ( "log" "os" "strings" "text/template" )

func main() { // First we create a FuncMap with which to register the function. funcMap := template.FuncMap{ // The name "title" is what the function will be called in the template text. "title": strings.Title, }

// A simple template definition to test our function.
// We print the input text several ways:
// - the original
// - title-cased
// - title-cased and then printed with %q
// - printed with %q and then title-cased.
const templateText = `

Input: {{printf "%q" .}} Output 0: {{title .}} Output 1: {{title . | printf "%q"}} Output 2: {{printf "%q" . | title}} `

// Create a template, add the function map, and parse the text.
tmpl, err := template.New("titleTest").Funcs(funcMap).Parse(templateText)
if err != nil {
    log.Fatalf("parsing: %s", err)
}

// Run the template to verify the output.
err = tmpl.Execute(os.Stdout, "the go programming language")
if err != nil {
    log.Fatalf("execution: %s", err)
}

}

template里可以传入FuncMap,里面是转换函数。

然后在模板里调用转换函数转换

karl_zhao
karl_zhao · #4 · 7年之前
jarlyynjarlyyn #3 回复

我给的链接下面就是 example啊,这个也需要我贴么…… package main import ( "log" "os" "strings" "text/template" ) func main() { // First we create a FuncMap with which to register the function. funcMap := template.FuncMap{ // The name "title" is what the function will be called in the template text. "title": strings.Title, } // A simple template definition to test our function. // We print the input text several ways: // - the original // - title-cased // - title-cased and then printed with %q // - printed with %q and then title-cased. const templateText = ` Input: {{printf "%q" .}} Output 0: {{title .}} Output 1: {{title . | printf "%q"}} Output 2: {{printf "%q" . | title}} ` // Create a template, add the function map, and parse the text. tmpl, err := template.New("titleTest").Funcs(funcMap).Parse(templateText) if err != nil { log.Fatalf("parsing: %s", err) } // Run the template to verify the output. err = tmpl.Execute(os.Stdout, "the go programming language") if err != nil { log.Fatalf("execution: %s", err) } } template里可以传入FuncMap,里面是转换函数。 然后在模板里调用转换函数转换

谢谢了,问题已经解决了。 我这边访问不了golang.org呢

jarlyyn
jarlyyn · #5 · 7年之前
karl_zhaokarl_zhao #4 回复

#3楼 @jarlyyn 谢谢了,问题已经解决了。 我这边访问不了golang.org呢

那你必须找个方法访问,不然怎么做后续的开发啊……

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传