Giter Club home page Giter Club logo

Comments (9)

flosch avatar flosch commented on May 12, 2024

Have you tried it with the latest master? I tried your example code and pongo2 panics with the following (correct) error:

panic: [Execution Error in <string> | Line 1 Col 38 (<Token Typ=Identifier (4) Val='Test' Line=1 Col=38>)] Function return type of 'Test.MyFunc' must be of type *Value.

All functions called by pongo2 must be of type func(*pongo2.Value, ....) *pongo2.Value. Your function would therefore look like this:

func (t *Test) MyFunc() *pongo2.Value {
    return pongo2.AsValue("My Func Result")
}

The result is the following:

$ go run test-pongo2.go 
Child: Test Child | MyFunc: My Func Result

Hope this helps.

from pongo2.

flosch avatar flosch commented on May 12, 2024

Just to clarfiy: You can return whatever type you want through pongo2.AsValue(). pongo2 relies on its own little type system and therefore requires all argument types/return types to be a *pongo2.Value.

The pongo2.Value documentation: http://godoc.org/github.com/flosch/pongo2#Value

A working example from the tests: https://github.com/flosch/pongo2/blob/master/pongo2_template_test.go#L34

from pongo2.

phcostabh avatar phcostabh commented on May 12, 2024

Thak you. It did help.

Now my only issue is to figure out a way to keep my code dry as I need to use MyFunc an other places other then in the template, where I can't rely on pongo2.Value.

For now I'm gonna wrap MyFunc in the PrintMyFunc function so that I can retrun pongo2.Value form it.

from pongo2.

flosch avatar flosch commented on May 12, 2024

I'll keep that in mind and think about that. Maybe I can make pongo2 more flexible in calling such functions.

Update: I created issue #9 for that.

from pongo2.

flosch avatar flosch commented on May 12, 2024

BTW, you can use pongo2.Values outside pongo2 if that makes your life simpler. For example,

func (t *Test) MyFunc() *pongo2.Value {
    return pongo2.AsValue("My Func Result")
}

you can simply call yourTestInstance.MyFunc().String() to get your string back (Bool(), Integer(), etc. does work too). You can even use MyFunc().Interface() to convert your value back to the original type (for example testInstance.MyFunc().Instance().(*myStruct)). Have a look on the documentation on which methods are supported on pongo2.Values.

But I understand that will only be a workaround for you.

from pongo2.

flosch avatar flosch commented on May 12, 2024

I pushed two commits to allow arbitrary function argument types and return types. Does this work for you?

from pongo2.

phcostabh avatar phcostabh commented on May 12, 2024

I'm gonna test it right now.

from pongo2.

phcostabh avatar phcostabh commented on May 12, 2024

Worked like a charm! Thak you a lot!

from pongo2.

flosch avatar flosch commented on May 12, 2024

You're welcome!

from pongo2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.