can I display parameters value ($_POST, $_GET, $_SESSION, $_SESSION) using twig template engine component

For $_POST variables use this :

{{ app.request.parameter.get("page") }}

For $_GET variables use this :

{{ app.request.query.get("page") }}

For $_COOKIE variables use this :

{{ app.request.cookies.get("page") }}

For $_SESSION variables use this :

{{ app.request.session.get("page") }}

The only variables available in Twig are:

  • the ones you pass through the second parameter of Twig_Environment#render()
  • the ones you pass by calling Twig_Environment#addGlobal()

If you wish to have a page variable available, add "page" => $_GET["page"] to the second parameter of render.

If you wish to have the complete $_GET superglobal available, add "GET" => $_GET to the second parameter of render.


You cannot access raw PHP values in TWIG so you must attached anything that you need to the twig object.

Here is an example of attaching the $_GET, $_POST and $_SESSION to twig.

//initialise your twig object
$loader = new \Twig_Loader_Filesystem(__PATH_TO_TEMPLATES_FOLDER__');
$this->twig = new \Twig_Environment($loader);

$this->twig->addGlobal('_session', $_SESSION);
$this->twig->addGlobal('_post', $_POST);
$this->twig->addGlobal('_get', $_GET);

Now lets assume you have a value called username stored in each of the above ($_GET, $_POST and $_SESSION) You can now access all of these inside your template as follows.

 {{ _session.username }}
 {{ _post.username }}
 {{ _get.username }}

Hope this helps

Tags:

Php

Twig