Q
Q
Qixing2015-02-12 08:30:22
symfony
Qixing, 2015-02-12 08:30:22

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'

Registering a service
crbr.currency_rate:
        class: CrbrBundle\Service\CurrencyRate
        arguments:
            - '@redcode.currency.rate.converter'

Sevris itself
<?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;
    }

}

Actually, the question is how in my
globals setup config:
rate: Is it correct to pass the rate value by calling my service with the necessary action?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-02-12
@Qixing

I want to set up a global array to manipulate currency rates in all twig templates.

Or maybe you don't want to?
In any case, it's better to look towards extensions for twig and registering your function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question