Created
October 1, 2019 19:29
-
-
Save rkueny/043f5c87d3cf38b752a334e88aead12a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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']), | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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