Created
October 1, 2019 19:27
-
-
Save rkueny/1202b35ab026e6fffe4cc72f8fcb4dab 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 Twig\Extension\AbstractExtension; | |
use Twig\TwigFunction; | |
class AppExtension extends AbstractExtension | |
{ | |
public function getFunctions() | |
{ | |
return [ | |
new TwigFunction('area', [$this, 'calculateArea']), | |
]; | |
} | |
public function calculateArea(int $width, int $length) | |
{ | |
return $width * $length; | |
} | |
} |
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\AbstractExtension; | |
use Twig\TwigFilter; | |
class AppExtension extends AbstractExtension | |
{ | |
public function getFilters() | |
{ | |
return [ | |
new TwigFilter('price', [$this, 'formatPrice']), | |
]; | |
} | |
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