douxing6434 2012-08-15 10:50
浏览 6
已采纳

Silex Twig Library的视图/资源位置

Normally, for a Silex project, I would have top-level directories like:

- app/
   - views/
- src/
- vendor/
- web/

Now, some of my classes may call $app['twig']->render(...) and it will pull out a view from the app/views folder.

If I extract a library to be more reusable, across multiple projects, where should I keep its view files, and how do I instruct Twig to look there?

The same question applies to graphics/stylesheets, etc which I would normally put in web/.

Surely they have to be within vendor/my-lib somewhere to allow Composer to cleanly install the files? Is there a common/best-practice way to do this?

Update For reference, here's what I ended up doing:

<?php

// in my \Silex\ServiceProviderInterface ...

/**
 * @var \Twig_Environment $twig
 */
$twig = $app['twig'];

// Add the paths to our twig templates here
$fsLoader = new \Twig_Loader_Filesystem(array(
    __DIR__.'/views/'
));

$twig->setLoader(new \Twig_Loader_Chain(array($twig->getLoader(), $fsLoader)));

Thanks.

  • 写回答

1条回答 默认 最新

  • dongnvwang8591 2012-08-15 11:09
    关注

    I store the views under src/{Library}/{Class}/View/

    I set the base path of Twig to the src

    $app->register(new TwigServiceProvider(), array(
        'twig.path' => array(
            __DIR__ . '/../src/{Library}/'
        ),
        'twig.options' => array('cache' => false, 'strict_variables' => true)
    ));
    

    and when calling render I pass in the path from that point

    $app['twig']->render('{Class}/View/{twigfile}.html.twig',$data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题