Skip to content

Instantly share code, notes, and snippets.

@rkueny
Created October 1, 2019 19:29
Show Gist options
  • Save rkueny/043f5c87d3cf38b752a334e88aead12a to your computer and use it in GitHub Desktop.
Save rkueny/043f5c87d3cf38b752a334e88aead12a to your computer and use it in GitHub Desktop.
<?php
namespace App\Twig;
use App\Twig\AppRuntime;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
class AppExtension extends AbstractExtension
{
public function getFilters()
{
return [
// the logic of this filter is now implemented in a different class
new TwigFilter('price', [AppRuntime::class, 'formatPrice']),
];
}
}
<?php
namespace App\Twig;
use Twig\Extension\RuntimeExtensionInterface;
class AppRuntime implements RuntimeExtensionInterface
{
public function __construct()
{
// this simple example doesn't define any dependency, but in your own
// extensions, you'll need to inject services using this constructor
}
public function formatPrice($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
{
$price = number_format($number, $decimals, $decPoint, $thousandsSep);
$price = '$'.$price;
return $price;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment