« Programmation PHP avec Symfony/Twig » : différence entre les versions
Contenu supprimé Contenu ajouté
Ligne 285 :
== Méthodes PHP appelables en Twig ==
En PHP, on peut définir des fonctions invocables en Twig, sous forme de fonction ou de filtre selon la méthode parente surchargée. Exemple :
<syntaxhighlight lang=php>
class TwigExtension extends AbstractExtension
return [▼
{
new Twig_SimpleFunction('getPrice', [$this, 'getPrice']),▼
public function getFilters(): array
{
▲ return [
new TwigFilter('getPrice', [$this, 'getPrice']),
];
}
public function getFunctions(): array
{
return [
▲ new Twig_SimpleFunction('getPrice', [$this, 'getPrice']),
];
}
public function getPrice($value): string
{
return number_format(2, ',', ' ') }} €;
}
}
</syntaxhighlight>
|