doumi1884 2013-05-08 07:50
浏览 6
已采纳

基于路径的Twig重载

This is a bit complicated to explain. My use-case is as follows:

Let's say I have the following filesystem structure:

/client/name/web/dashboard.twig <- extends dashboard.twig - file might not exist
/app/name/web/dashboard.twig    <- extends dashboard.twig - file might not exist
/web/dashboard.twig             <- extends layout.twig    - file always exists
/web/layout.twig                <- the base template      - file always exists

Assuming I run $twig->render('dashboard.twig');, I want it to start with the topmost dashboard.twig and keep on going sequentially as in the order specified above.

The current problem is that twig ends up in an infinite loop trying to load the first template file each time. In the recipes section, there's an example to avoid this which makes use paths. This cannot work in my case because of several issues:

  1. The developer of the /client/name/web/dashboard.twig should not be aware of the /app/name/web/dashboard.twig file.
  2. It's still not possible to identify the name path segment and as such, the path concept can't work.

The only solution I can think of is to somehow plug into twig and tell it to "pop out" template files as soon as they're loaded, to eliminate the infinite loop.

This part of the API docs seems useful: http://twig.sensiolabs.org/api/master/Twig_Loader_Filesystem.html

  • 写回答

1条回答 默认 最新

  • douneiben2240 2013-05-09 07:17
    关注

    Thanks to an answer from Charles, I've been able to find a solution to my problem.

    The main idea is two overload the "extends" Twig tag with code that adds file names of extended files to the name which is later on passed to the loader.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。