I have a use-case for adding a ucfirst
filter in my twig template and I have it working just fine by using this:
$this->getServiceLocator()
->get('Twig_Environment')
->addFilter(
new \Twig_SimpleFilter(
'ucfirst',
'ucfirst'
)
);
However I am curious if it's possible to add this filter via the module.config.php
?
Perhaps something like this:
'zfctwig' => [
'environment_options' => [
'cache' => 'data/cache/twig',
'debug' => true
],
'extensions' => [
'Twig_Extension_Debug'
],
'Twig_Environment' => [
'filters' => [
'ucfirst' => 'ucfirst'
]
]
]
I know this snippet is wrong, but if it is possible, what would the configuration look like?
I'm currently using ZF2 with the ZfcTwig module.
Nope, it's not possible to register new filter just with configuration in ZfcTwig module.
But it is recommended to create project extension (twig doc) and put all your project specific filters (tags, tests, etc) there.
Example of this project extension:
<?php
namespace Application\Twig;
use Twig_Extension;
use Twig_SimpleFilter;
class ApplicationExtension extends Twig_Extension
{
public function getFilters()
{
return [
new Twig_SimpleFilter('ucfirst', 'ucfirst'),
];
}
public function getTests()
{
return [
// ...
];
}
public function getFunctions()
{
return [
// ...
];
}
}
and then you can just register this extension to the ZfcTwig configuration:
'zfctwig' => [
'extensions' => [
\Application\Twig\ApplicationExtension::class,
],
]
If you need then add some another filter or function, you can simply add it to this extension and start using it.