fabpot/Twig — pull request #826 by fabpot (2012-09-06 15:32:35+00:00)
Title: Allow PHP callables to be used to register filters, tests, and functions

Registering a new filter, a new test, or new function is harder than it should be (you need to create an instance of a class, and registering a method is only possible if you are using an extension).

This PR adds the possibility to just pass any valid PHP callable instead, like a method on any object:

```php
$twig->addFilter('foo1', array(new FooFilter(), 'foo'));
$twig->addFunction('foo1', array(new FooFilter(), 'foo'));
$twig->addTest('foo1', array(new FooFilter(), 'foo'));

class FooFilter
{
public function foo($foo)
{
return strtoupper($foo);
}
}
```

You can also use a closure (if you are using PHP 5.3+):

```php
$twig->addFilter('foo', function ($foo) { return strtoupper($foo); });
$twig->addFunction('foo', function ($foo) { return strtoupper($foo); });
$twig->addTest('foo', function ($foo) { return strtoupper($foo); });
```

This new feature will probably lower the barrier of entry when someone wants to customize Twig.

Don't have a look the code for now (this is just a prototype that tries to keep BC). I will probably come up with something cleaner, but I first want to gather feedback on the idea.