如何使用来自数据库的值为 Twig 模板定义全局变量?
How to define global variables for Twig templates with values coming from the DB?
我想为 twig 定义一个全局变量,它可以从任何模板访问。
我可以在symfony的config/packages/twig.yaml
中创建一个全局变量,但我需要它是从数据库中获取的值。
在 twig 的文档中它说使用这个代码:
$twig = new Twig_Environment($loader);
$twig->addGlobal('name', 'value');
我应该在哪里使用此代码以便此变量可用于每个模板?
一种相对简洁的方法是添加一个 event subscriber,它会在实例化控制器之前全局注入变量。
正如其中一条评论所建议的那样,在控制器中执行的问题是您的全局变量根本不会 global,而是仅为该控制器定义。
你可以这样做:
// src/Twig/TwigGlobalSubscriber.php
use Doctrine\ORM\EntityManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
class TwigGlobalSubscriber implements EventSubscriberInterface {
/**
* @var \Twig\Environment
*/
private $twig;
/**
* @var \Doctrine\ORM\EntityManager
*/
private $manager;
public function __construct( Environment $twig, EntityManager $manager ) {
$this->twig = $twig;
$this->manager = $manager;
}
public function injectGlobalVariables( GetResponseEvent $event ) {
$whatYouWantAsGlobal = $this->manager->getRepository( 'SomeClass' )->findBy( [ 'some' => 'criteria' ] );
$this->twig->addGlobal( 'veryGlobal', $whatYouWantAsGlobal[0]->getName() );
}
public static function getSubscribedEvents() {
return [ KernelEvents::CONTROLLER => 'injectGlobalVariables' ];
}
}
我故意模糊了 DB 交互,因为只有您确切知道要从那里检索什么。但是您要在此订户上注入 EntityManager
,因此只需检索适当的存储库并执行适当的搜索即可。
完成后,您可以简单地从您的树枝模板中执行以下操作:
<p>I injected a rad global variable: <b>{{ veryGlobal }}</b></p>
Symfony 5.4
service.yml
:
App\Twig\TwigGlobalSubscriber:
tags:
- { name: kernel.event_listener, event: kernel.request }
SysParams - 我的服务从数据库中获取数据
src\Twig\TwigGlobalSubscriber.php
<?php
namespace App\Twig;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
use App\Service\SysParams;
class TwigGlobalSubscriber implements EventSubscriberInterface {
private $twig;
public function __construct( Environment $twig, SysParams $sysParams ) {
$this->twig = $twig;
$this->sysParams = $sysParams;
}
public function injectGlobalVariables() {
$base_params = $this->sysParams->getBaseForTemplate();
foreach ($base_params as $key => $value) {
$this->twig->addGlobal($key, $value);
}
}
public static function getSubscribedEvents() {
return [ KernelEvents::CONTROLLER => 'injectGlobalVariables' ];
}
public function onKernelRequest()
{
}
}
我想为 twig 定义一个全局变量,它可以从任何模板访问。
我可以在symfony的config/packages/twig.yaml
中创建一个全局变量,但我需要它是从数据库中获取的值。
在 twig 的文档中它说使用这个代码:
$twig = new Twig_Environment($loader);
$twig->addGlobal('name', 'value');
我应该在哪里使用此代码以便此变量可用于每个模板?
一种相对简洁的方法是添加一个 event subscriber,它会在实例化控制器之前全局注入变量。
正如其中一条评论所建议的那样,在控制器中执行的问题是您的全局变量根本不会 global,而是仅为该控制器定义。
你可以这样做:
// src/Twig/TwigGlobalSubscriber.php
use Doctrine\ORM\EntityManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
class TwigGlobalSubscriber implements EventSubscriberInterface {
/**
* @var \Twig\Environment
*/
private $twig;
/**
* @var \Doctrine\ORM\EntityManager
*/
private $manager;
public function __construct( Environment $twig, EntityManager $manager ) {
$this->twig = $twig;
$this->manager = $manager;
}
public function injectGlobalVariables( GetResponseEvent $event ) {
$whatYouWantAsGlobal = $this->manager->getRepository( 'SomeClass' )->findBy( [ 'some' => 'criteria' ] );
$this->twig->addGlobal( 'veryGlobal', $whatYouWantAsGlobal[0]->getName() );
}
public static function getSubscribedEvents() {
return [ KernelEvents::CONTROLLER => 'injectGlobalVariables' ];
}
}
我故意模糊了 DB 交互,因为只有您确切知道要从那里检索什么。但是您要在此订户上注入 EntityManager
,因此只需检索适当的存储库并执行适当的搜索即可。
完成后,您可以简单地从您的树枝模板中执行以下操作:
<p>I injected a rad global variable: <b>{{ veryGlobal }}</b></p>
Symfony 5.4
service.yml
:
App\Twig\TwigGlobalSubscriber:
tags:
- { name: kernel.event_listener, event: kernel.request }
SysParams - 我的服务从数据库中获取数据
src\Twig\TwigGlobalSubscriber.php
<?php
namespace App\Twig;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
use App\Service\SysParams;
class TwigGlobalSubscriber implements EventSubscriberInterface {
private $twig;
public function __construct( Environment $twig, SysParams $sysParams ) {
$this->twig = $twig;
$this->sysParams = $sysParams;
}
public function injectGlobalVariables() {
$base_params = $this->sysParams->getBaseForTemplate();
foreach ($base_params as $key => $value) {
$this->twig->addGlobal($key, $value);
}
}
public static function getSubscribedEvents() {
return [ KernelEvents::CONTROLLER => 'injectGlobalVariables' ];
}
public function onKernelRequest()
{
}
}