Pass parameter to all views

前端 未结 4 1502
青春惊慌失措
青春惊慌失措 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:22

    The simplest solution is to embed controllers from your templates/layout. But beware that subrequests are costly and can affect performance significantly. If at some point you'll notice that the dev version of your app is slow as hell, then know that the reason is probably several subrequests on each request.

    The next solution is Twig extensions. In most cases you'll want functions. You could call it like:

    {{ user_info(user) }}
    

    I started with embedding controllers first, but my dev version reached the point when most pages on my site were timing out in 30 seconds. I didn't know the reason first, but as soon as I found it out, I replaced all subrequsts with Twig extensions. Since then the performance is back to normal.

提交回复
热议问题