I need to do a custom filter that will convert a price to the user currency preference. So i have a table updated everyday with the latest exchange rates.
I plan to call the filter this way {{ '200'|price }} or {{ '200'|price('USD') }}
The filter would first take the user preference from a cookie.
Then it will query the latest exchange rate based of the currency code and convert the actual price.
My question is, is it all right to do a database query in a twig extension (filter here).
I might have to call this filter 10 times in a page. Does that mean 10 more queries?
What better ways would you advise?
Yes, if you don't define the cache configuration for Doctrine, it means that you will send request to the database to fetch the data everytime.
You can choose between querying the exchange rates, store them in the cache yourself or using cache feature of Doctrine to do it.
Symfony 2 cache Doctrine query results
Both ways are better than querying the data from database directly every single time.