RSS Feed

[Symfony 2] Twig – Global Variables


Schon mal gefragt, welche globalen Variablen in der Twig-Extension eines Symfony 2 (Standard Distri)-Projektes zur Verfügung stehen?
Schaut man sich die Klasse GlobalVariables im Namespace Symfony\Bundle\FrameworkBundle\Templating an (diese Klasse ist Teil des Framework-Bundles, d.h., Gleiches gilt ebenfalls für PHP als Template-Maschine), wird einiges klarer. Weiter unten eine Liste der Assessoren, die die Klasse bereitstellt:

    Symfony\Component\Security\Core\SecurityContext|void         getSecurity()
    Symfony\Component\Security\Core\User\(Advanced)UserInterface getUser()
    Symfony\Component\HttpFoundation\Request|void                getRequest()
    Symfony\Component\HttpFoundation\Session|void                getSession()
    string                                                       getEnvironment()
    bool                                                         getDebug()

In Twig liegt der Container im “app”-Namespace, “Übersetzt” nach Twig heißt das ganze also:

Viel Spaß beim Templates-Bauen ;)

7 responses to “[Symfony 2] Twig – Global Variables”

  1. […] This is a summary of global twig variables and its corresponding classes. Tweet This entry was posted in Development, symfony by Luis Cordova. Bookmark the permalink. […]

  2. goyote says:

    why did you write the title in English, and the article in some other language?

    • The master of the internet told me to do so, but never reported back since then :(

      Perhaps i decide to translate the article in the near future, in the meantime i suggest to investigate the contents of the class Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables which should make things clear.

  3. Matt Weisman says:

    In the time you commented back to the first response, you could have translated this post :(

    Thanks for pointing out the GlobalVariables.php file though!

  4. Adam Pugh says:

    Once you find the GlobalVariables.php all you have to do is make functions inside that file (with the same naming conventions) and Symfony will Automagically allow the use of those functions inside of Twig.


    public function getHttp()
    if ($request = $this->getRequest()) {
    return $request->getHttpHost();

    use {{ app.http }} in your Twig

  5. david says:

    as this is the first hit in google for “symfony global twig” i want to point out that the idea to hack GlobalVariables.php is a very ugly hack indeed, as you will lose things when you update symfony.

    have a look at this cookbook entry how to do things properly:

  6. joshi says:

    Indeed. The above article is not about how to inject new globals but how to identify existing ones – written in a time where there was only little documentation about twig integration into symfony (performed by twig bundle/twig bridge).

    So please consider this post as outdated and refer to the official symfony templating documentation and related cookbook articles.

    For myself i would always prefer writing a twig extension in favor of depositing globals into the “twig”-section of config.yml.

Leave a Reply

Your email address will not be published. Required fields are marked *