kisphp / twig-extensions
Kisphp twig extensions
Installs: 5 936
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Forks: 0
pkg:composer/kisphp/twig-extensions
Requires
- php: >=7.4|>=8.0
- twig/twig: ~2.4|~3.0
Requires (Dev)
- phpunit/phpunit: ~9.5
- symfony/var-dumper: ~3.4|~5.0|~6.0
This package is auto-updated.
Last update: 2025-10-05 11:45:10 UTC
README
Installation
composer require kisphp/twig-extensions
Usage
Create twig Filter
<?php
namespace AppBundle\Twig\Filters;
use Kisphp\Twig\AbstractTwigFilter;
class DemoFilter extends AbstractTwigFilter
{
/**
* @return string
*/
protected function getExtensionName()
{
return 'demo';
}
/**
* @return \Closure
*/
protected function getExtensionCallback()
{
return function ($name) {
// add here your filter logic
};
}
}
<?php
namespace AppBundle\Twig\Functions;
use Kisphp\Twig\AbstractTwigFunction;
class DemoFunction extends AbstractTwigFunction
{
/**
* @return string
*/
protected function getExtensionName()
{
return 'demo';
}
/**
* @return \Closure
*/
protected function getExtensionCallback()
{
return function ($name) {
// add here your function logic
};
}
}
Register new twig extensions
<?php
namespace AppBundle\Twig;
use AppBundle\Twig\Functions\DemoFunction;
use AppBundle\Twig\Filters\DemoFilter;
use Twig\Extension\AbstractExtension;
class TwigExtension extends AbstractExtension
{
public function getFunctions()
{
return [
DemoFunction::create(),
];
}
public function getFilters()
{
return [
DemoFilter::create(),
];
}
}
If your function of filter has dependencies then you can instantiate them like this:
public function getFunctions()
{
$myCustomDependency = 'object, variable, array or what ever you need here';
return [
(new DemoFunction($myCustomDependency))->getExtension(),
];
}