Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

twig

I had tried do like this :

{{ $_GET['page'] }}

but that still didn't work.

like image 678
antoniputra Avatar asked Oct 23 '13 23:10

antoniputra


4 Answers

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") }}
like image 60
Fouad Fodail Avatar answered Oct 31 '22 22:10

Fouad Fodail


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.

like image 42
Maerlyn Avatar answered Oct 31 '22 22:10

Maerlyn


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

like image 33
Matthew Cawley Avatar answered Oct 31 '22 22:10

Matthew Cawley


Another working solution @ 2019

app.request.get('page')
app.request.request.get('page')
like image 5
sadiq Avatar answered Oct 31 '22 22:10

sadiq