dongque3797 2014-08-27 14:29
浏览 72
已采纳

Twig和Symfony2的模板层次结构

I'm building a /app/Resources/base.html.twig with common features for almost all the development I have since for example all use Twitter Bootstrap, Select2 and some common libraries. Then for any project I created a bundle called TemplateBundle and inside it I have a template called layout.html.twig which extends from base.html.twig. Having this as a start point I have some doubts around my approach:

  • In base.html.twig I handle assets with Assetic and I have this lines:

    {% block stylesheets %}
        {% stylesheets 
                'bundles/template/css/bootstrap.min.css' 
                'bundles/template/css/bootstrap-theme.min.css' 
                'bundles/template/css/font-awesome.min.css' 
                'bundles/template/css/select2.css' 
                'bundles/template/css/select2-bootstrap.css' 
                'bundles/template/css/bootstrapValidator.min.css' 
                'bundles/template/css/datepicker.css' 
                'bundles/template/css/datepicker3.css' 
                'bundles/template/css/styles.css' 
           filter='cssrewrite' 
        %}
        <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}
    {% endblock %}
    

I want to make base.html.twig as extendable as can be and in the code above not all of my sites uses datepicker.css or styles.css since this are part of the current one or in other cases files names changed between sites, so my question regarding this is: how I handle this in layout.html.twig? For example in Site1 all the templates will extends from layout.html.twig and then in this layout I can set the use of datepicker and styles but for Site2 I could define to use just styles and so on, so how to handle this in layout.html.twig maintaining assetic?

  • As you may notice all my assets resides on TemplateBundle but what happens if tomorrow I called this Template1Bundle? Then I should take care of base.html.twig and change any assetic path, how do you handle this? I mean where do you put your common files as this one for example?

Understand my question?

  • 写回答

1条回答 默认 最新

  • duanhe0817825 2014-08-28 03:37
    关注

    The base template will be very complicated. I will rather to have multi layout instead. Since you have everything in a bundle.

    Another option is use have the common ones in base.html.twig. and extends at layout.html.twig with {{ parent() }} which will print the base.html.twig block content, but you will lose the front end performance in production.

    base.html.twig

    {% block stylesheets %}
        {% stylesheets 'bundles/template/css/bootstrap.min.css' filter='cssrewrite' %}
        <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}
    {% endblock %}
    

    layout.html.twig

    {% block stylesheets %}
        {{ parent() }}
        {% stylesheets 
            'bundles/template/css/datepicker.css' 
            'bundles/template/css/datepicker3.css'
        filter='cssrewrite' %}
        <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}
    {% endblock %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据