O
O
ol sm2017-07-24 18:38:53
symfony
ol sm, 2017-07-24 18:38:53

Symfony3+Twig How to make a global variable?

Good evening.
Please tell me how to implement a global variable, for example {{ tags.name }}, with the output of a specific value from the database in any template.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor, 2017-07-24
@ol_efk

Make a service that can give you the data you need upon request of the desired method.
And inject the service into twig.
In config.yml:

twig:
    globals:
        your_service: '@your_service'

In twig, you can just pull a {{ your_service.someMethod() }}
bit of documentation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question