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 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败