将配置值传递给twig

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将配置值传递给twig相关的知识,希望对你有一定的参考价值。

我正在使用symfony 4.0和created a custom config file。我将此配置文件值设置为参数,以便我可以在控制器中获取它们。但我想将它们传递给twig。我怎么能这样做?可以直接在config.yml中分配应用程序配置,还是可以使用twig扩展名来实现?我找到了getGlobals(),但这已被弃用了。仍然用枝条功能做到了,但对我来说似乎不是一个好方法。

最好,谢谢!

AppExtension.php

namespace AppBundleDependencyInjection;

use SymfonyComponentHttpKernelDependencyInjectionExtension;
use SymfonyComponentDependencyInjectionContainerBuilder;
use SymfonyComponentDependencyInjectionLoaderYamlFileLoader;
use SymfonyComponentConfigFileLocator;


class AppExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = $this->getConfiguration($configs, $container);
        $config = $this->processConfiguration($configuration, $configs);
        $container->setParameter('config', $config); 
    }

}

?>

Resources / config / app.yml:

app:
  document_access:
    payment:
        bank_details:
          account_owner: xxx
          iban: Iban
          bic: Bic

ConfigExtension.php

namespace AppBundleTwig;

use SymfonyComponentDependencyInjectionContainerInterface;

class ConfigExtension extends Twig_Extension
{

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function getFunctions(){
        return array(
            new Twig_SimpleFunction('config', array($this, 'getConfig')),
        );
    }

    public function getConfig()
    {
        return $this->container->getParameter('config');
    }
}
答案

将所需的变量设置为环境变量和参数。

然后你可以将参数注入控制器或将变量注册为twig globals。

调节器

class AngularController
{
    protected $twig;
    protected $accountOwner;
    protected $iban;
    protected $bic;

    /**
     * AngularController constructor.
     * @param $accountOwner
     * @param $iban
     * @param $bic
     */
    public function __construct(Twig_Environment $twig, $accountOwner, $iban, $bic)
    {
        $this->twig=$twig;
        $this->accountOwner = $accountOwner;
        $this->iban = $iban;
        $this->bic = $bic;
    }


    /**
     * @Route("/uri", name="your_route")
     */
    public function yourActionAction()
    {
        $data=["account_owner"=>$this->accountOwner, "iban"=>$this->iban, "bic"=>$this->bic];
        $content=$this->twig->render('Bundle:Bundle:index.html.twig', $data);
        $response=new Response($content);
        return $response;
    }
}

controller.angular:
class: DJABundleControllerAngularController
arguments:
    - "@twig"
    - %bank_account_owner%
    - %bank_iban%
    - %bank_bic%

Twig全球

# app/config
twig:
    [...]
    globals:
        account_owner: %bank_account_owner%
        iban: %bank_iban%
        bic: "%bank_bic%"

用于控制器和全局变量的twig

Bank account owner: {{ account_owner }}
Bank account iban: {{ iban }}
Bank account bic: {{ bic }}

以上是关于将配置值传递给twig的主要内容,如果未能解决你的问题,请参考以下文章

如何将活动值传递给片段[重复]

如何将php变量传递给twig模板

将统一值传递给顶点和片段着色器

Kotlin - 如何将空值传递给片段 newInstance 方法?

markdown 将Gravity Forms作为变量传递给Twig / Timber

markdown 将Gravity Forms作为变量传递给Twig / Timber