I had tried do like this :
{{ $_GET['page'] }}
but that still didn't work.
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:
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
Another working solution @ 2019
app.request.get('page')
app.request.request.get('page')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With