Symfony 2 how to import LESS files from another bundle

假装没事ソ 提交于 2019-12-05 08:02:56
jacobmaster

I think that you must use the paths from web/bundles dir.

i'm importing files this way:

i have 2 less files:

  • src/Acme/FirstBundle/Resources/public/less/style1.less
  • src/Acme/SecondBundle/Resources/public/less/style2.less

I want to import style1.less into style2.less

style2.less:

@import "../../acmefirst/less/style1";

using: cssrewrite filer, lessphp

also remember to refer the LESS files using their actual, publicly-accessible path: http://symfony.com/doc/current/cookbook/assetic/asset_management.html#including-css-stylesheets

Kaizoku Gambare

Here is a complete working example at least on Symfony 2.8. This example using Assetic and is supposed to work with embed file in your css.

Here the arborescence

/app
/src
---/Acme
------/MyBundle
---------/Ressources
------------/public
---------------/css
------/MyOtherBundle
---------/Ressources
------------/public
---------------/css
/web
---/bundles
------/acmemybundle
------/acmemyotherbundle
---/css
------/built

So in let's say /src/Acme/MyBundle/Ressources/public/css/main.scss is the file with all the declaration that I want to import in my other bundle (in my case I use sass but it's the same with less).

In /src/Acme/MyOtherBundle/Ressources/public/css/mycss.scss I'll do :

@import "../../../../MyBundle/Resources/public/css/main";

This refer to the classic physical location of the file, so your IDE will find it.

Now the interesting part. We want to compile, minify, and rename all the scss file into only one css file. We can do this with Assetics.

In a twig file where you load your css (in my case /app/Ressources/views/css.html.twig).

    {% stylesheets
    filter='compass'
    filter='?uglifycss'
    filter='cssrewrite'
    output='css/built/myMinifiedAndCompiledSass.css'
    'bundles/mybundle/css/*.scss'
    'bundles/myotherbundle/css/*.scss'
   %}
    <link rel="stylesheet" type="text/css" href="{{ asset_url }}">
    {% endstylesheets %}

==> Here you have to refer the file from the /web directory (so using 'bundles/acmemybundle..' syntax. You need to install the asset in symlink mode. (php app/console asset:install --symlink)

==> You can put whatever you want in output filemane and location since you stay in the web directory.

And finaly in your conf.yml

# Assetic Configuration
assetic:
    filters:
        cssrewrite: ~
        sass:    ~
        compass:
             load_paths:
                  - "/usr/bin/compass"
                  - "%kernel.root_dir%/../src/Acme/MyBundle/Resources/public/css/"
        uglifycss:
            bin: %kernel.root_dir%/../node_modules/.bin/uglifycss
        uglifyjs2:
            bin: %kernel.root_dir%/../node_modules/.bin/uglifyjs

The important part here is the load_paths in compass. In the basic set up you have compass : ~ You need to change it for :

  compass:
                 load_paths:
                      - "/usr/bin/compass"
                      - "%kernel.root_dir%/../src/Acme/MyBundle/Resources/public/css/"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!