Skip to content
  • Homepage
  • HTML
  • CSS
  • Symfony
  • PHP
  • How to
  • Contact
  • Donate

Teach Developer

Articles, Guides & Tips

How to Define Global Variables for Twig Templates in Symfony 6

Home  »  Symfony   »   How to Define Global Variables for Twig Templates in Symfony 6
Posted on May 21, 2023May 21, 2023
798

Sometimes we may need to inject the same variable in all the Twig templates. It can be done on each controller by passing a variable to template. However, it is not a good solution. This tutorial shows how to define global variables for Twig templates in Symfony 6 application.

Global variables for Twig templates can be defined using globals option under the twig key:

config/packages/twig.yaml

twig:
  globals:
    tracking_id:'XX-YY-ZZ'

In our case, the variable tracking_id will be injected automatically into all the Twig templates.

templates/test/index.html.twig

{{ tracking_id }}
Symfony Tags:PHP

Post navigation

Previous Post: How to Make Toggles With React Hooks
Next Post: How to Check Twig Version in Symfony

Related Posts

  • How to split a string into an array with Twig
  • How to use events listeners and Event Subscriber in Symfony
  • How to Symfony parameters and environment variables use
  • How to Symfony Request
  • How to Check Twig Version in Symfony

Categories

  • Codeigniter (3)
  • CSS (11)
  • eCommerce (1)
  • Framework (1)
  • Git (3)
  • How to (43)
  • HTML (5)
  • JavaScript (15)
  • Jquery (7)
  • Laravel (1)
  • Linux (4)
  • Magento-2 (1)
  • Node js (4)
  • Others (2)
  • PHP (11)
  • React (13)
  • Server (1)
  • SSH (3)
  • Symfony (6)
  • Tips (16)
  • Top Tutorials (10)
  • Ubuntu (3)
  • Vue (1)
  • Wordpress (7)

Latest Posts

  • What is SSH in Linux?
  • How to Delete Files in Ubuntu Command Line
  • How to Deploy a React application on a cPanel
  • How to use events listeners and Event Subscriber in Symfony
  • How to Convert PHP CSV to JSON

WEEKLY TAGS

AJAX (1) Codeigniter (1) Javascript (11) JQuery (1) PHP (16) Programming (1) React (3) Symfony (1)

Random Post

Dealing with deprecations
How to display the last updated date of a post in WordPress
How to Search Recently Modified Files in Linux
How to split a string into an array with Twig
How to PHP Get Max Value From Array

Quick Navigation

  • About
  • Contact
  • Privacy Policy

© Teach Developer 2021. All rights reserved