Pass parameter to all views

前端 未结 4 1468
青春惊慌失措
青春惊慌失措 2021-01-14 12:02

I want to display the username/last connection date/time and some other info on all of my Twig views (which all extend from a common Twig layout).

How can I achieve

4条回答
  •  北恋
    北恋 (楼主)
    2021-01-14 12:40

    I don't know if it was available when this question was posted in 2012, but I'd use Twig Globals.

    From http://twig.sensiolabs.org/doc/advanced.html#globals :

    Globals

    A global variable is like any other template variable, except that it's available in all templates and macros:

    $twig = new Twig_Environment($loader);
    $twig->addGlobal('text', new Text());
    

    You can then use the text variable anywhere in a template:

    {{ text.lipsum(40) }}
    

    I put the code in some place where it will get called each time, like the controller constructor or something like that.

提交回复
热议问题