如何在symfony 2中包含资源文件夹中的css

时间:2022-10-16 12:43:47

I hvae the css located in

我找到了位于的CSS

app/Resources/FOSUserbundle/css

应用程序/资源/ FOSUserbundle / CSS

How can i include that in my twig template

我怎样才能在我的树枝模板中包含它

The reason i am putting css there is that my all overridden FOSUser templates in in that folder. So i want to keep css , js images all in there so that if i need to use in other website i just copy that folder

我把css放在那里的原因是我所有被覆盖的FOSUser模板都在该文件夹中。所以我想保留css,js图像,所以如果我需要在其他网站使用我只是复制该文件夹

3 个解决方案

#1


6  

I'm not quite sure how you would include that in your TWIG templates, but ...

我不太确定你将如何在TWIG模板中包含它,但是......

1) I put the resources I use in several bundles / projects in the web/ directory. Then you can reference then like this:

1)我将我使用的资源放在web /目录中的几个bundle / projects中。然后你可以像这样引用:

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

2) If you need to override the FOSUserbundle anyway, you can put the resources inside the inheriting bundle, referencing them like this:

2)如果你需要覆盖FOSUserbundle,你可以将资源放在继承包中,引用它们如下:

{% javascripts '@YourBundle/Resources/public/js/scripts.js' %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}

#2


6  

What you have done is perfectly all right.

你所做的一切都很好。

Just do the following:-

只需执行以下操作: -

$ app/console assets:install web

It will install the assets in the public "web" directory, where the assets should technically be, to be used with your Twig Templates.

它将资产安装在公共“web”目录中,这些资产在技术上应该与您的Twig模板一起使用。

Assets can then be used within Twig templates like this:-

然后可以在Twig模板中使用资产,如下所示: -

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


    {% block javascripts %}
        <script src="{{ asset('/js/main.js') }}" type="text/javascript"></script>
    {% endblock %}

#3


3  

Although the question suggests and override case, it also aks for a case where the css or js are in a common folder, away from the bundle structure.

虽然问题建议并覆盖了大小写,但它也要求css或js位于公共文件夹中,远离捆绑结构。

One answer is to use the available twig variables. For example if I have one css file located here

一个答案是使用可用的树枝变量。例如,如果我在这里有一个css文件

app/Resources/views/clientSite/customFolder/css/mycss.css

I can load in any template using something like this (note the example overrides the full stylesheets block, but not required, the stylesheet twig tag can be added in any block):

我可以使用类似的东西加载任何模板(注意示例覆盖完整的样式表块,但不是必需的,样式表twig标记可以添加到任何块中):

{% block stylesheets %}
    {{ parent() }}
    {% stylesheets '%kernel.root_dir%/Resources/views/clientSite/customFolder/css/mycss.css'
    %}
    <link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
    {% endstylesheets %} 
{% endblock %}

Also remember to execute the assetic:dump command, so symfony knows it had to publish the css to the web/css folder with all the other files.

还记得执行assetic:dump命令,因此symfony知道它必须将css发布到包含所有其他文件的web / css文件夹。

#1


6  

I'm not quite sure how you would include that in your TWIG templates, but ...

我不太确定你将如何在TWIG模板中包含它,但是......

1) I put the resources I use in several bundles / projects in the web/ directory. Then you can reference then like this:

1)我将我使用的资源放在web /目录中的几个bundle / projects中。然后你可以像这样引用:

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

2) If you need to override the FOSUserbundle anyway, you can put the resources inside the inheriting bundle, referencing them like this:

2)如果你需要覆盖FOSUserbundle,你可以将资源放在继承包中,引用它们如下:

{% javascripts '@YourBundle/Resources/public/js/scripts.js' %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}

#2


6  

What you have done is perfectly all right.

你所做的一切都很好。

Just do the following:-

只需执行以下操作: -

$ app/console assets:install web

It will install the assets in the public "web" directory, where the assets should technically be, to be used with your Twig Templates.

它将资产安装在公共“web”目录中,这些资产在技术上应该与您的Twig模板一起使用。

Assets can then be used within Twig templates like this:-

然后可以在Twig模板中使用资产,如下所示: -

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


    {% block javascripts %}
        <script src="{{ asset('/js/main.js') }}" type="text/javascript"></script>
    {% endblock %}

#3


3  

Although the question suggests and override case, it also aks for a case where the css or js are in a common folder, away from the bundle structure.

虽然问题建议并覆盖了大小写,但它也要求css或js位于公共文件夹中,远离捆绑结构。

One answer is to use the available twig variables. For example if I have one css file located here

一个答案是使用可用的树枝变量。例如,如果我在这里有一个css文件

app/Resources/views/clientSite/customFolder/css/mycss.css

I can load in any template using something like this (note the example overrides the full stylesheets block, but not required, the stylesheet twig tag can be added in any block):

我可以使用类似的东西加载任何模板(注意示例覆盖完整的样式表块,但不是必需的,样式表twig标记可以添加到任何块中):

{% block stylesheets %}
    {{ parent() }}
    {% stylesheets '%kernel.root_dir%/Resources/views/clientSite/customFolder/css/mycss.css'
    %}
    <link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
    {% endstylesheets %} 
{% endblock %}

Also remember to execute the assetic:dump command, so symfony knows it had to publish the css to the web/css folder with all the other files.

还记得执行assetic:dump命令,因此symfony知道它必须将css发布到包含所有其他文件的web / css文件夹。