symfony - Import a file with constants in Twig? -
Is it possible to import a file with a bunch of constants in another TIG file?
Like:
{% set VAR1 = stable ("...: var1")%} {% set VAR2 = static ("...: var2" )%} {% Set VAR3 = stable ("...: var3")%} ... And then, in these other files, is this constant used?
I have tried to do include , import , usage and embed . None of these seems to fit in this situation.
With the design, you can not import the variable from another reference, because {% include% } without copying your current reference will be passed by the reference, not by context. The {% macro%} and parameters as _context variable, so you can not create a new variable in the context from within a macro And then your macro file {% import%} However, you can create variables from the extended file, and use them in the hair file.
For example:
a.html.twig {% set test = 'hello!' %} b.html.twig {% extends 'a.html.twig'%} { {Test}} will appear "hello!" When b.html.twig, or any other files, provides an ..twig extension.
If, by design, you can not raise a parent template in your application, you implement to create a Twigg extension and getGlobals () method. Example (for me Fugh / Testbndl, change according to your needs all namespaces): :
PHP fuzz / Testbndl / Tvig / Extensions / App Gallows Extension. FTP & lt; php namespace Fuz \ TestBundle \ Twig \ extension; Class AppGlobalsExtension \ Twig_Extension {public function getGlobals () {extends return array ( 'var1' = & gt; \ Fuz \ TestBundle \ components \ Services :: SOME_CONSTANT_A, 'var2' = & gt; \ Fuz \ TestBundle \ components \ Services: : SOME_CONSTANT_B, 'Var3' = & gt; \ Fuz \ TestBundle \ Component \ Service :: SOME_CONSTANT_C, // ...); } Public Function getName () {Return 'Epiglabals'; }} configuration:
YML Fuz / TestBundle / resources / config / services.yml parameter: fuz_tools.twig.appglobals_extension.class: Fuz \ TestBundle \ branch \ extensions \ AppGlobalsExtension services: Fuz_toolsktwigkappglobals: class: '% fuz_tools.twig.appglobals_extension.class%' Tags: - {name: twig. Expansion} You can now use {{var1}} , {{var2}} ... your application ideas Anywhere in
Comments
Post a Comment