Comments (9)
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.
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.
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.
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.
BTW, you can use pongo2.Value
s 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.Value
s.
But I understand that will only be a workaround for you.
from pongo2.
I pushed two commits to allow arbitrary function argument types and return types. Does this work for you?
from pongo2.
I'm gonna test it right now.
from pongo2.
Worked like a charm! Thak you a lot!
from pongo2.
You're welcome!
from pongo2.
Related Issues (20)
- Cannot using template.Fromfile HOT 1
- Context key invalid HOT 1
- How to convert golang array to javascript array HOT 1
- Why this is no document about how to use it ?? HOT 8
- Struct field names in templates / contexts
- Data race in `pongo2.FromFile()` method HOT 5
- Potential security issue HOT 3
- date filter does not work as expected HOT 4
- Array in yml file as context is not supported
- Macro that invokes other macros does not work when imported HOT 1
- Not compatible with GCCGO 13.0.0 HOT 1
- How can pongo2 does not escape chars which except TokenSymbols?
- Bug: random order for `for` loop. HOT 2
- Include statement doesn't work inside for loop
- Returning only a partial block
- Potential BC break on master (missing var comparison)
- BC break between v4 and v6 (string/num adding/concatenating)
- question about split filter on new line
- need strings function HOT 1
- What tags deos pongo uses of declared structs struct?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pongo2.