Symfony2:如何与Twig模板继承一起适当地包含资产? [英] Symfony2: How to properly include assets in conjunction with Twig template inheritance?

查看:79
本文介绍了Symfony2:如何与Twig模板继承一起适当地包含资产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Symfony 2.1.0开发Web应用程序.

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:

  • 应用/资源/观看次数/
    • base.html.twig:基本模板,包含 title 样式表 body 块. li>
    • app/Resources/views/
      • base.html.twig: base template, containing title, stylesheets and body blocks.
      • layout.html.twig:布局模板(扩展基本模板),将主样式表附加到 stylesheet 块,并覆盖 body 块,包括 navigation.html.twig 并定义 content
      • layout-admin.html.twig:与上述相同,但包括 navigation-admin.html.twig
      • src/我的/PageBundle/资源/视图/主要
        • 标准模板,扩展布局模板并覆盖其 content
        • 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
          • 管理模板.与上述相同,但是扩展了管理布局模板.
          • main.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.

          现在,问题是,在layout.html中,我添加了以下内容:

          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 %}
          

          但是asset('css/main.css')只是链接到/css/main.css,而./app/console assets:install将资产安装在web/bundles/mypagebundle/中.我不喜欢这样的事实,我的捆绑包名称是公开可见的(这可能使用户怀疑我正在使用Symfony,而且我喜欢保留网页的内部,内部和内部).是否可以更改assets:install用来安装资产的目录?对我来说,将资源手动安装到web/中似乎很麻烦.

          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/.

          我还考虑使用Assetic进行资产管理,因为我个人喜欢自动缩小脚本/样式表并将它们全部存储在一个文件中的可能性.但是,我听说如果您在不同级别包含样式表是不可能的,即它不适用于三级继承系统.可以解决这个问题吗?另外,使用Assetic是否可以让我向公众隐藏我的捆绑商品名称?

          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?

          推荐答案

          使用Assetic可以解决您的所有问题.

          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

          可以,但是它将为每个级别生成一个css文件(实际上就像asset()一样.)

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

          示例:

          布局:

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

          子模板:

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

          结果:

          <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="..." />
          

          这篇关于Symfony2:如何与Twig模板继承一起适当地包含资产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆