Super globals and constants in twig

Even supper globals like _POST, _GET and CONSTANTs should be rendered as variables from controller to twig and they are not directly accessible in twig?

Show us your code.

I did not yet code it. I just ask super globals and constants are not directly accessible in twig code? Please let me know.

Hello nimasdj,
Try this:

{# $_POST #}
{{ app.request.parameter.get("page") }}  

{# $_GET #}
{{ app.request.query.get("page") }} 

Cheers

  • Nicolas

How about constants? I do use language constants. Plz advice

Try this:

{{ constant('Namespace\\Classname::CONSTANT_NAME') }}

Sorry I don’t understand why namespace and class? Language files contain just simple constants. Please clarify.

Do you speak about Twig Constants or PHP Constants?

I mean php constants. How to access them in twig templates?
I am not familiar with twig constants. What are them?

Here is an example who explains what I have written above:

Link

Cheers

My language files just contain simple php constants, not a class. How to access this kind of php constants that are not class constants?

Can you show me 2/3 lines of this file?

define(ā€œCONFIRMEDā€, ā€œConfirmedā€);
define(ā€œCANCELLEDā€, ā€œCancelledā€);
How to access them?

any advice yet about constants?
also in tp of my controller I added:

session_start();
$_SESSION['username'] = 'blah';

then in twig

{{ app.session.get('username') }}

but this is blak. what should I do?

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

Thanks but I am still waiting your kind help about constants.

I do not really understand why you manage Languages by this way.
I have to see more code to help you.

In my old code I had a language.php file that just define constants e.g.
define(ā€œCONFIRMEDā€, ā€œConfirmedā€);
when I want to show a page first I require that language.php file to get defined constants then in my template I had:
Status: <?=CONFIRMED?>
This way I could change the language without editing template rather than hard-coding in template like:
Status: Cofirmed.
Now how to get this php constants in twig? please advice.

In top of controller I added:
session_start();
$_SESSION[ā€˜username’] = ā€˜blah’;
and from symphony profiler toolbar I ass ā€œHas Session: Yesā€
But I don’t get the session variable with:
{{ app.request.session.get(ā€œusernameā€) }}
plase advice.

I invite you to use the Symfony2 Session Interface (HttpFoundation Component)

Have a glance to : Link