0
votes

I'm using symfony 4.0 and created a custom config file. I'm setting this config file values to a parameter, so that I can get them in a controller. But I want to pass/access them also to twig. How could I do this? Is is possible to assign the app config directly in config.yml or could I do it with a twig extension? I found getGlobals(), but this is deprecated. Still did it with a twig function, but seems not a good way to me.

Best and thank you!

AppExtension.php:

namespace AppBundle\DependencyInjection;

use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;


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 AppBundle\Twig;

use Symfony\Component\DependencyInjection\ContainerInterface;

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');
    }
}
1

1 Answers

0
votes

Set the variables required as environtment variables and parameters.

Then you can do it injecting the parameters into you controlller or registering the variables as twig globals.

Controller

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: DJABundle\Controller\AngularController
arguments:
    - "@twig"
    - %bank_account_owner%
    - %bank_iban%
    - %bank_bic%

Twig globals

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

Usage in twig for both controller and globals

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