Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
|
prog:symfony:extensions:twig:variables [01/11/2022 17:54] thierry créée |
prog:symfony:extensions:twig:variables [01/11/2022 18:01] (Version actuelle) thierry [Source & Ressources] |
||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| ====== Les variables dans TWIG ====== | ====== Les variables dans TWIG ====== | ||
| ===== Récupérer les variables d'environnement ===== | ===== Récupérer les variables d'environnement ===== | ||
| + | |||
| + | Cela se passe dans le fichier ''config\packages\twig.yaml'' | ||
| + | |||
| + | Ci dessous cela permet de mappé la variable d'environnement ''APP_ENV'' (qui se trouve dans le fichier ''.env'') sur la variable Twig ''app_environment''. | ||
| + | <code yaml config\packages\twig.yaml> | ||
| + | twig: | ||
| + | ... | ||
| + | # Register your global variables under the globals key: | ||
| + | globals: | ||
| + | # Obtain it from the .env file: | ||
| + | app_environment: '%env(APP_ENV)%' | ||
| + | |||
| + | </code> | ||
| + | |||
| + | Exemple d'utilisation dans un fichier Twig : | ||
| + | <code twig templates\base.html.twig> | ||
| + | <!DOCTYPE html> | ||
| + | <html> | ||
| + | <head> | ||
| + | <meta charset="UTF-8"> | ||
| + | <title> | ||
| + | {% block title %}Welcome!{% endblock %} | ||
| + | </title> | ||
| + | {% if app_environment == 'prod' %} | ||
| + | <link rel="stylesheet" href="{{ asset('css/prod.css') }}"> | ||
| + | {% else %} | ||
| + | <link rel="stylesheet" href="{{ asset('css/dev.css') }}"> | ||
| + | {% endif %} | ||
| + | </head> | ||
| + | <body> | ||
| + | {% block body %}{% endblock %} | ||
| + | </body> | ||
| + | </html> | ||
| + | </code> | ||
| ====== Source & Ressources ====== | ====== Source & Ressources ====== | ||
| + | * [[https://ourcodeworld.com/articles/read/1177/how-to-retrieve-env-variables-directly-from-a-twig-view-in-symfony-5]] | ||