本文整理汇总了Golang中html/template.FuncMap函数的典型用法代码示例。如果您正苦于以下问题:Golang FuncMap函数的具体用法?Golang FuncMap怎么用?Golang FuncMap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。

在下文中一共展示了FuncMap函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: TestTemplateFuncMap_join

func TestTemplateFuncMap_join(t *testing.T) {
    app := kocha.NewTestApp()
    funcMap := template.FuncMap(app.Template.FuncMap)
    tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{join .Arr .Sep}}`))
    var buf bytes.Buffer
    for _, v := range []struct {
        Arr    interface{}
        Sep    string
        expect string
    }{
        {[]int{1, 2, 3}, "&", "1&2&3"},
        {[2]uint{12, 34}, " and ", "12 and 34"},
        {[]string{"alice", "bob", "carol"}, ", ", "alice, bob, carol"},
        {[]string(nil), "|", ""},
        {[]bool{}, " or ", ""},
        {[]interface{}{"1", 2, "three", uint32(4)}, "-", "1-2-three-4"},
        {[]string{"あ", "い", "う", "え", "お"}, "_", "あ_い_う_え_お"},
        {[]string{"a", "b", "c"}, "∧", "a∧b∧c"},
    } {
        buf.Reset()
        if err := tmpl.Execute(&buf, v); err != nil {
            t.Error(err)
            continue
        }
        actual := buf.String()
        expect := v.expect
        if !reflect.DeepEqual(actual, expect) {
            t.Errorf(`{{join %#v %#v}} => %#v; want %#v`, v.Arr, v.Sep, actual, expect)
        }
    }
}

开发者ID:naoina,项目名称:kocha,代码行数:31,代码来源:template_test.go

示例2: NewTemplate

func NewTemplate() *template.Template {
    return template.New("").Funcs(template.FuncMap(map[string]interface{}{
        "q": func(fieldName string) (interface{}, error) {
            return nil, errors.New("The q template dummy function was called")
        },
    }))
}

开发者ID:ivartj,项目名称:norske-irc-kanaler.com,代码行数:7,代码来源:site.go

示例3: TestTemplate_FuncMap_url

func TestTemplate_FuncMap_url(t *testing.T) {
    app := kocha.NewTestApp()
    funcMap := template.FuncMap(app.Template.FuncMap)

    func() {
        tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{url "root"}}`))
        var buf bytes.Buffer
        if err := tmpl.Execute(&buf, nil); err != nil {
            panic(err)
        }
        actual := buf.String()
        expected := "/"
        if !reflect.DeepEqual(actual, expected) {
            t.Errorf("Expect %q, but %q", expected, actual)
        }
    }()

    func() {
        tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{url "user" 713}}`))
        var buf bytes.Buffer
        if err := tmpl.Execute(&buf, nil); err != nil {
            panic(err)
        }
        actual := buf.String()
        expected := "/user/713"
        if !reflect.DeepEqual(actual, expected) {
            t.Errorf("Expect %v, but %v", expected, actual)
        }
    }()
}

开发者ID:naoina,项目名称:kocha,代码行数:30,代码来源:template_test.go

示例4: TemplatesFuncs

// TemplatesFuncs adds functions that will be available to all templates.
// It is legal to overwrite elements of the map.
func TemplatesFuncs(funcs FuncMap) {
    if templates == nil {
        panic(errNoTemplatesDir)
    }

    templates.Funcs(template.FuncMap(funcs))
}

开发者ID:volatile,项目名称:response,代码行数:9,代码来源:helper.go

示例5: TestTemplateFuncMap_flash

func TestTemplateFuncMap_flash(t *testing.T) {
    c := newTestContext("testctrlr", "")
    funcMap := template.FuncMap(c.App.Template.FuncMap)
    for _, v := range []struct {
        key    string
        expect string
    }{
        {"", ""},
        {"success", "test succeeded"},
        {"success", "test successful"},
        {"error", "test failed"},
        {"error", "test failure"},
    } {
        c.Flash = kocha.Flash{}
        c.Flash.Set(v.key, v.expect)
        tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(fmt.Sprintf(`{{flash . "unknown"}}{{flash . "%s"}}`, v.key)))
        var buf bytes.Buffer
        if err := tmpl.Execute(&buf, c); err != nil {
            t.Error(err)
            continue
        }
        actual := buf.String()
        expect := v.expect
        if !reflect.DeepEqual(actual, expect) {
            t.Errorf(`{{flash . %#v}} => %#v; want %#v`, v.key, actual, expect)
        }
    }
}

开发者ID:naoina,项目名称:kocha,代码行数:28,代码来源:template_test.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng