-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Extending twig - defining functions" documentation is misleading #308
Comments
The doc is true for the sentence you copied from it. |
And allowing any objects is not possible at all due to the way the compiled template works. They would not have access to the object whereas the Twig extension is available through the environment. The |
Then that should be clarified in the docs then. Assumptions and implications have no place in documentation. |
The $this psuedo variable actually has special meaning in php: it is the current instance of an object. This knowledge makes the documentation much clearer. |
@web-dev obviously. It is, however, meaningless without _context_. |
your doc is more misleading dude |
Looking at the documentation for defining functions for use inside of twig templates, the documentation is very misleading.
"Adding a function is similar to adding a filter. This can be done by calling the addFunction() method on the Twig_Environment instance:"
What the variable
$this
is supposed to be is not even stated at all -- not a type, not an interface, nothing. By omitting these details, the documentation instead implies that the first param will accept any object that is passed.The source code itself, however, shows this:
https://github.com/fabpot/Twig/blob/master/lib/Twig/Function/Method.php#L23
public function __construct(Twig_ExtensionInterface $extension, $method, array $options = array())
So, there's clearly a problem here. The way it is explained in the documentation is that you can provide an object and name a method, and twig will basically create a new "function" inside the templates that aliases to that method; looking at the source code however, you instead have to code a new object that implements a specific interface, fulfill the methods in the interface, and then work in your own code around that. This is much, much more complex.
The documentation and the reality of the implementation for Twig_Function_Method are worlds apart and definitely need rectified.
As a user, I'd like to see the implementation be modified to mirror what the documentation states (and not the other way around); having to do that much more work to import a function that is supposed to refer to an already-defined object method is cumbersome and ridiculous.
The text was updated successfully, but these errors were encountered: