dongzong1866 2016-09-17 12:30
浏览 81

如何从pongo模板调用Go函数

I need to create JSON data using few keys of map and need to incorporate into html generated. I am using pongo2 library and want to write custom filter to achieve the same.

<script> {{ CategoryMapping|MycustomFilter }} </script>

and coded custom filter like below.

func init() {

    pongo2.RegisterFilter("superfilter", GetCategoryJsonData)


}

func GetCategoryJsonData(CatAttributeMapping *map[string]interface{}, param *int) (*string, *error) {
.....
}

But I am getting below error.

src/util/TemplateFilters.go:10: cannot use GetCategoryJsonData (type func(*int, *int) (*string, *error)) as type pongo2.FilterFunction in argument to pongo2.RegisterFilter

I am following below documentation - https://godoc.org/github.com/flosch/pongo2#FilterFunction

I am new to go and unable to understand what wrong I am doing here. Please guide me for the same.

  • 写回答

1条回答 默认 最新

  • doujiexin1136 2016-09-18 03:32
    关注

    The problem is that your filter function does not accept or return the right types to match what pongo2 is requiring. Let's walk through the docs and see what they want.

    First, take a look at the godoc for RegisterFilterFunction. It says

    func RegisterFilter(name string, fn FilterFunction)
    

    This is in the pongo2 package so you should read this as RegisterFilter is a function that accepts two arguments and returns no values. The first argument name is of the builtin type string and the second argument fn is of the type pongo2.FilterFunction. But what is a pongo2.FilterFunction? Well clicking on it we see further down in the doc

    type FilterFunction func(in *Value, param *Value) (out *Value, err *Error)
    

    In Go you can make your own types based on any other types including functions. So what pongo2 has done is to create a named type called FilterFunction that is any func which accepts two arguments (both of type *pongo2.Value) and returns two values (one of type *pongo2.value and one of type *pongo2.Error).

    To bring it all together we would do something like this:

    package main
    
    import (
        "fmt"
        "log"
        "strings"
    
        "github.com/flosch/pongo2"
    )
    
    func init() {
        pongo2.RegisterFilter("scream", Scream)
    }
    
    // Scream is a silly example of a filter function that upper cases strings
    func Scream(in *pongo2.Value, param *pongo2.Value) (out *pongo2.Value, err *pongo2.Error) {
        if !in.IsString() {
            return nil, &pongo2.Error{
                ErrorMsg: "only strings should be sent to the scream filter",
            }
        }
    
        s := in.String()
        s = strings.ToUpper(s)
    
        return pongo2.AsValue(s), nil
    }
    
    func main() {
        tpl, err := pongo2.FromString("Hello {{ name|scream }}!")
        if err != nil {
            log.Fatal(err)
        }
        // Now you can render the template with the given
        // pongo2.Context how often you want to.
        out, err := tpl.Execute(pongo2.Context{"name": "stack overflow"})
        if err != nil {
            log.Fatal(err)
        }
        fmt.Println(out) // Output: Hello STACK OVERFLOW!
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类