Comments (7)
You're welcome! Yes, a better documentation is on my TODO list (#36). I will definitely work on better docs. :-)
from pongo2.
It should work (and it works in the tests at least, for example here: https://github.com/flosch/pongo2/blob/master/pongo2_template_test.go#L44 and https://github.com/flosch/pongo2/blob/master/template_tests/complex.tpl#L22). Can you please provide more code (how you're creating the object and how you pass it to the template etc.) or - even better - a working minimal example where this does not work for you so it's possible for me to reproduce it? Thanks!
Are you sure you're passing a pointer to a Flash
object to your context?
from pongo2.
The flash object is not a pointer, but I still expected it to be able to call the method and be implicitly converted.
from pongo2.
Method with pointer receivers are only part of the method set of *T (not T). If you have a method with a pointer receiver the object must be of type *Flash in order to access this method.
See https://golang.org/ref/spec#Method_sets:
A type may have a method set associated with it. The method set of an interface type is its interface. The method set of any other type T consists of all methods declared with receiver type T. The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T). Further rules apply to structs containing anonymous fields, as described in the section on struct types. Any other type has an empty method set. In a method set, each method must have a unique non-blank method name.
from pongo2.
I would have expected this not to work then:
package main
import (
"fmt"
)
type Blah struct {
Name string
}
func (this *Blah) Test() string {
return this.Name
}
func main() {
b := Blah{Name: "Something"}
fmt.Printf("Al: %s\n", b.Test())
}
Here b is not a pointer, yet it can call the Test() function. I would have the same expectation with the code in this issue.
from pongo2.
The reason for that is https://golang.org/ref/spec#Calls:
A method call x.m() is valid if the method set of (the type of) x contains m and the argument list can be assigned to the parameter list of m. If x is addressable and &x's method set contains m, x.m() is shorthand for (&x).m()
pongo2 uses the reflect
package do get the method set and it follows the before-mentioned definition of a method set. So yes, it works in Go, but it does not work in pongo2 which follows the strict method set definition. Your solution is to pass a pointer to an object (which is usually a good idea anyway).
from pongo2.
Good to know and thanks for the clarification :). Just thought some of pongo2 users may get into the same issue, so perhaps we could document this.
from pongo2.
Related Issues (20)
- TemplateSet.FromCache would get error result when use multi loaders
- 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
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.