普通网友 2012-09-16 17:48
浏览 84
已采纳

Symfony2:如何与Twig模板继承一起正确包含资产?

I'm currently developing a web application using Symfony 2.1.0.

I've read through the Templating chapter of the book and I'm trying to include assets (right now, it's just a single stylesheet) in my web pages.

I'm using the Three-level inheritance system that is mentioned in the book, and my application structure currently looks like this:

  • app/Resources/views/
    • base.html.twig: base template, containing title, stylesheets and body blocks.
  • src/My/PageBundle/Resources/views
    • layout.html.twig: layout template (extending the base template), appending the main stylesheet to the stylesheet block, and overwriting the body block, including navigation.html.twig and defining a content block
    • layout-admin.html.twig: same thing as above, but including navigation-admin.html.twig
    • src/My/PageBundle/Resources/views/Main
      • standard templates, extending the layout template and overwriting its content block
    • src/My/PageBundle/Resources/views/Administration
      • administration templates. Same thing as above, but extending the administration layout template.
  • src/My/PageBundle/Resources/public/css
    • main.css: main stylesheet

As you can see, I have put the stylesheet in my bundle. I don't know whether this is good practice or not.

Now, the thing is, in layout.html I added this:

{% block stylesheets %}
    {{ parent() }}

    <link rel="stylesheet" type="text/css" href="{{ asset('css/main.css)' }}" />
{% endblock %}

But asset('css/main.css') is just linking to /css/main.css, whereas ./app/console assets:install installs the assets in web/bundles/mypagebundle/. I don't like the fact that this way, my bundle name would be publicly visible (which could make users suspect I'm using Symfony, and I like keeping the internals of my webpage, well, internal). Is it possible to change the directory where assets:install would install the assets? It seems tedious to me to manually install the assets in web/.

I'm also thinking about using Assetic for asset management, as I personally like the possibility to automatically minify my scripts/stylesheets and store them all together in one file. However, I hear that this is not possible if you include stylesheets at different levels, i.e. it wouldn't work with the three-level inheritance system. Is it possible to work around that? Also, would using Assetic enable me to hide my bundle name from the public?

  • 写回答

1条回答 默认 最新

  • dongxu1029 2012-09-16 19:21
    关注

    Using assetic would address all your issues.

    I hear that this is not possible if you include stylesheets at different levels, i.e. it wouldn't work with the three-level inheritance system

    You can, but it will generate a css file for each level (just like with asset(), actually).

    Example:

    layout:

    {% block stylesheets %}
        {{ parent() }}
        {% stylesheets 'main.css' %}
            <link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
        {% endstylesheets %}
    {% endblock %}
    

    sub-template:

    {% block stylesheets %}
        {{ parent() }}
        {% stylesheets 'sub.css' %}
            <link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
        {% endstylesheets %}
    {% endblock %}
    

    result:

    <link rel="stylesheet" type="text/css" href="..." />
    <link rel="stylesheet" type="text/css" href="..." />
    

    Alternatively sub-template can completly override the stylesheets block, so that only one stylesheet is generated (but it's less dry):

    {% block stylesheets %}
        {% stylesheets 'main.css' 'sub.css' %}
            <link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
        {% endstylesheets %}
    {% endblock %}
    

    result (in production / non debug):

    <link rel="stylesheet" type="text/css" href="..." />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)