Answer the question
In order to leave comments, you need to log in
How to pass value from service to twig.global.var in symfony2?
I want to set up a global array to manipulate currency rates in all twig templates.
Configuration: config.yml
twig:
#...
globals:
rate: '@crbr.currency_rate'
crbr.currency_rate:
class: CrbrBundle\Service\CurrencyRate
arguments:
- '@redcode.currency.rate.converter'
<?php
namespace CrbrBundle\Service;
use Doctrine\ORM\EntityManager;
class CurrencyRate
{
public function __construct($currency)
{
$this->$converter = $currency;
}
public function getRate()
{
$rate = array();
$currencyDiff = $this->container->getParameter('currencyDiff');
$converter = $this->get('redcode.currency.rate.converter');
$rate['usd'] = $this->convert('USD', 'RUB', 1) + $currencyDiff;
$rate['jpy'] = $this->convert('JPY', 'RUB', 1) * 100 + $currencyDiff;
return $rate;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question