문제

I've just started looking into Twig and I'm wondering how I would accomplish the following.

I have a variable $logged_in that I need to have access to in every single page on my site, I was hoping that rather than passing this to the twig renderer every single time in the data array, there would be a way for me to declare this somewhere, and for every template to have access to it.

Do I need to build an extension to accomplish this / or is it even possible? I have looked through every page of the documentation but I'm having trouble having tried to extend the base template as described here...

Twig Documentation | Recipes | Making the Templates aware of the Context Dead link

Is this the right approach?

Thanks

도움이 되었습니까?

해결책

Just read about the new features in the 1.0RC release which should help.

Taken from the blogpost:

Globals:

PHP

// a global can be a constant
$twig->addGlobal('pi', 3.14);

// or any other valid PHP expression, like an object
$twig->addGlobal('request', new Request());

Template

{{ pi }}

{{ request.params('name') }}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top