twig - 通过核心扩展更改 number_format 过滤器的默认值
问题描述
我想更改number_format
on twig 的默认设置。在他们的文档中,他们展示了如何做到这一点
$twig = new \Twig\Environment($loader);
$twig->getExtension(\Twig\Extension\CoreExtension::class)->setNumberFormat(3, '.', ',');
我的问题是使用 Symfony 时我可以在哪里插入此代码?
解决方案
您可以在 twig.yaml 配置中更改此设置。
# app/config/packages/twig.yaml
twig:
number_format:
decimals: 2
decimal_point: ','
thousands_separator: '.'
或者如果您可以使用事件订阅者手动将其更改为内核请求事件。
<?php
namespace App\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
use Twig\Extension\CoreExtension;
class TwigSettingListener implements EventSubscriberInterface
{
private $twig;
public function __construct(Environment $twig)
{
$this->twig = $twig;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
public function onKernelRequest(RequestEvent $event): void
{
$this->twig->getExtension(CoreExtension::class)
->setNumberFormat(3, '.', ',');
}
}
推荐阅读
- python - 使用 QSortFilterProxyModel 时的 pyQt5 段错误
- r - 在 ggplot2 中调整极坐标图的开始
- ruby - 世博会挂在 CocaPods 安装上
- spring - HttpSessionSecurityContextRepository - 无 HttpSession,仅在 Chrome 中
- git - 如何指定将使用哪个 ssh 密钥 `go get`
- networking - 向代理服务器发送请求时如何获取目标端口和 IP?
- javascript - 在js中改变vmax
- asp.net-core - 在 asp.net core web api 中将请求作为 Multipart 表单数据值发送
- python - 关于 Python3 语义
- php - ErrorException:md5_file(/tmp/phptdn2a80.30601700 1609791282):无法打开流:没有这样的文件或目录