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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵