dongnaosuan5407 2018-04-02 11:15
浏览 34
已采纳

模板和自定义功能; 错误:在<“ funcName”>执行“ templName”不是定义函数

I got some text that I´m adding with template.AddParseTree method in order to append the template text, but there is a weir behaviour, the method is supossed to use it like this:

singleTemplate=anyTemplate
targetTemplate=*template.Must(targetTemplate.AddParseTree(e.Name, anyTemplate.Tree))

But is not working when singleTemplate has a funtion, by a weird reason it only works when I do this

singleTemplate=anyTemplate
targetTemplate=*template.Must(singleTemplate.AddParseTree(e.Name, anyTemplate.Tree))

But it must not work that way, because I won´t be able to append anything else You can try it here: https://play.golang.org/p/f5oXNzD1fKP

package main

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

var funcionIncremento = template.FuncMap{
    "inc": func(i int) int {
        return i + 1
    },
}

func main() {

    var strs []string
    strs = append(strs, "test1")
    strs = append(strs, "test2")
    //-----------------Not valid(it would)
    var probar1 = template.Template{}
    var auxiliar1 = template.Template{}
    auxiliar1 = *template.Must(template.New("test").Funcs(funcionIncremento).Parse(`
            {{range $index, $element := .}}
                Number: {{inc $index}}
            {{end}}
    `))

    probar1 = *template.Must(probar1.AddParseTree("test", auxiliar1.Tree))
    err := probar1.ExecuteTemplate(os.Stdout, "test", &strs)
    if err != nil {
        log.Println("Error1: ", err)
    }
//--------------------------------valid(it wouldn´t), because I wouldn´t be able to append
    var probar2 = template.Template{}
    var auxiliar2 = template.Template{}
    auxiliar2 = *template.Must(template.New("test").Funcs(funcionIncremento).Parse(`
            {{range $index, $element := .}}
                Number: {{inc $index}}
            {{end}}
    `))

    probar2 = *template.Must(auxiliar2.AddParseTree("test", auxiliar2.Tree))
    err = probar2.ExecuteTemplate(os.Stdout, "test", &strs)
    if err != nil {
        log.Println("Error2: ", err)
    }
    //-------------------------------------------
}
  • 写回答

1条回答 默认 最新

  • doubingjian2006 2018-04-02 12:02
    关注

    a.AddParseTree("t", b.Tree) adds only the parse.Tree of template b it does not add b's functions because they are not part of the parse.Tree type, they are part of the template.Template type.

    Any function that b needs to use, must also be added to a.

    var probar1 = template.New("pro").Funcs(funcionIncremento)
    var auxiliar1 = template.New("aux").Funcs(funcionIncremento)
    
    auxiliar1 = template.Must(auxiliar1.Parse(`
            {{range $index, $element := .}}
                Number: {{inc $index}}
            {{end}}
    `))
    
    probar1 = template.Must(probar1.AddParseTree("test", auxiliar1.Tree))
    err := probar1.ExecuteTemplate(os.Stdout, "test", &strs)
    if err != nil {
        log.Println("Error1: ", err)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘