简体   繁体   中英

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

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM