Skip to content

Instantly share code, notes, and snippets.

@rkueny
Created October 1, 2019 19:27
Show Gist options
  • Save rkueny/1202b35ab026e6fffe4cc72f8fcb4dab to your computer and use it in GitHub Desktop.
Save rkueny/1202b35ab026e6fffe4cc72f8fcb4dab to your computer and use it in GitHub Desktop.
<?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;
}
}
<?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