4

I have been trying for some time now to compile Bootstrap 3 in a Symfony 2 project on Windows. But I can't get it to work. My primary objective is to compile my very own LESS file. I called it "styles.less". In there, I want to be able to use bootstrap mixins like "make-xs-column" for example. So I need to import bootstrap.less for that.

Here is what I did so far:

In my composer.json, I added the bootstrap bundle:

{
    ... 
    "require": {
        ...
        "twitter/bootstrap": "v3.0.3"
    },
    ....
}

Since I want to use Bootstrap 3, I cannot use the lessphp filter, so I use the less filter instead. For that, I had to install nodejs, and then less (by running the command "npm install less -g"). Finally, I modified my config.yml like so:

assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    bundles:        [ JoePersonalWebSiteBundle ]
    filters:
        cssrewrite: ~
        less:
            node: "C:\\dev\\nodejs\\lessc.cmd"
            node_paths: 
                - "C:\\dev\\nodejs\\node_modules"
            apply_to: "\.less$"

Now, in my layout.html.twig, I added the following:

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

And in my "styles.less" file, I import"bootstrap.less" like so:

@import '../../../../../../vendor/twitter/bootstrap/less/bootstrap.less';

But I always get an error. In fact, even if my "styles.less" file is completely empty, I always get an error like this one:

[exception] 500 | Internal Server Error | Assetic\Exception\FilterException
[message] An error occurred while running:
&quot;C:\dev\nodejs\lessc.cmd&quot; &quot;C:\Users\joe\AppData\Local\Temp\assDE7E.tmp&quot;

Error Output:
[31mParseError: missing opening `{`[39m[31m in [39mC:\Users\joe\AppData\Local\Temp\assDE7E.tmp[90m on line 17, column 1:[39m
[90m16 });[39m
17 [0m[0m


[1] Assetic\Exception\FilterException: An error occurred while running:
&quot;C:\dev\nodejs\lessc.cmd&quot; &quot;C:\Users\joe\AppData\Local\Temp\assDE7E.tmp&quot;

Error Output:
[31mParseError: missing opening `{`[39m[31m in [39mC:\Users\joe\AppData\Local\Temp\assDE7E.tmp[90m on line 17, column 1:[39m
[90m16 });[39m
17 [0m[0m

I tried to create my own recess filter to use that instead of less (based on the work by boteeka found here). But that didn't work either. The less files never compile. Even an empty one, or a simple one.

Could someone please point me in the right direction? Is it possible on Windows, to compile Bootstrap 3 in a Symfony 2 project? If so, can someone give me the exact steps I should follow?

Jean-François Beauchef
  • 1,291
  • 2
  • 11
  • 21
  • What is the problem with lessphp on windows? Lessphp works for me well with Bootstrap 3.0.0 on windows. – stwe Dec 17 '13 at 18:45
  • @stwe : I had errors with lessphp. I don't remember the exact error, but on Twitter Bootstrap's web site, near the top of the "Getting Started" page, it says "For compiling LESS files into CSS, we only officially support Recess". That's why I tried to download Recess and create my own Recess filter. Are you able to compile "bootstrap.less"? Can you import it like I am trying to do? – Jean-François Beauchef Dec 17 '13 at 19:23
  • There is an issue with lessphp and Bootstrap > 3.0.3 https://github.com/leafo/lessphp/issues/503 So I still use version 3.0.0 – stwe Dec 17 '13 at 20:57
  • I would recommend using something like Grunt to watch your files and compile them when they're updated rather than having this baked into your application. Is your production environment going to be compiling LESS? – Seer Dec 17 '13 at 22:02
  • @Seer, I am not sure I follow. In my dev environment, assetic.use_controller is set to true, so everything is generated dynamically, while in prod, it is set to false. Then I run "`php app/console assetic:dump --env=prod --no-debug`" come deployment time in production, to generate everything. Isn't that the way to do it? That's how it is described in the [Asset Management](http://symfony.com/doc/current/cookbook/assetic/asset_management.html) section of Symfony's cook book. – Jean-François Beauchef Dec 18 '13 at 13:51
  • Yeah, that'd work too I guess, never mind then! :) – Seer Dec 19 '13 at 19:15

1 Answers1

2

I use lessphp in windows with Bootstrap v3.0.0. The original idea is from http://mossco.co.uk/symfony-2/symfony-2-and-bootstrap-3-assetic-config-and-base-html-template-file/

I have also added to the entries for the fonts, icons.

My composer.json

"require": {
    "components/jquery": "dev-master",
    "leafo/lessphp": "v0.4.0",
    "twbs/bootstrap": "v3.0.0",
},

For the config.yml copy 'cssembed' and 'yuicompressor' to '%kernel.root_dir%/Resources' /java/

My config.yml

    # Assetic Configuration
assetic:
    debug:          %kernel.debug%
    use_controller: false
    bundles:        [ ]
    java: C:\Program Files\Java\jre7\bin\java.exe
    filters:
        cssrewrite: ~
        cssembed:
            jar: %kernel.root_dir%/Resources/java/cssembed-0.4.5.jar
        yui_js:
            jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar
        lessphp:
            file: %kernel.root_dir%/../vendor/leafo/lessphp/lessc.inc.php
            apply_to: "\.less$"
    assets:
        jquery_js:
            inputs:
                - "%kernel.root_dir%/../components/jquery/jquery.min.js"
            filters: [?yui_js]
        bootstrap_js:
            inputs:
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/transition.js"
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/alert.js"
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/modal.js"
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/dropdown.js"
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/scrollspy.js"
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/tab.js"
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/tooltip.js"
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/popover.js"
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/button.js"
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/collapse.js"
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/carousel.js"
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/js/affix.js"
            filters: [?yui_js]
        bootstrap_less:
            inputs:
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/less/bootstrap.less"
            filters: [lessphp, cssembed]
        fonts_glyphicons_eot:
            inputs:
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/fonts/glyphicons-halflings-regular.eot"
            output: "fonts/glyphicons-halflings-regular.eot"
        fonts_glyphicons_svg:
            inputs:
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/fonts/glyphicons-halflings-regular.svg"
            output: "fonts/glyphicons-halflings-regular.svg"
        fonts_glyphicons_ttf:
            inputs:
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/fonts/glyphicons-halflings-regular.ttf"
            output: "fonts/glyphicons-halflings-regular.ttf"
        fonts_glyphicons_woff:
            inputs:
                - "%kernel.root_dir%/../vendor/twbs/bootstrap/fonts/glyphicons-halflings-regular.woff"
            output: "fonts/glyphicons-halflings-regular.woff"

And here my base.html.twig

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>{% block title %}Welcome!{% endblock %}</title>

        {% block stylesheets %}

            {% stylesheets '@bootstrap_less' combine=true %}
                <link href="{{ asset_url }}" type="text/css" rel="stylesheet">
            {% endstylesheets %}

        {% endblock %}

        <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
    </head>
    <body>
        {% block body %}{% endblock %}

        {% block javascripts %}

            {% javascripts '@jquery_js' '@bootstrap_js' filter='?yui_js' combine=true %}
                <script src="{{ asset_url }}"></script>
            {% endjavascripts %}

        {% endblock %}
    </body>
</html>

Somehow cssembed unnecessary or does not function properly and can be removed with this solution!

stwe
  • 1,262
  • 15
  • 18
  • Thanks @stwe, I will accept this as the answer. I just switched to "lessphp" in my stylesheets block, and switched to bootstrap 3.0.0 in composer, and it worked. It doesn't work with bootstrap 3.0.1 and above. Only with 3.0.0. Thanks again! – Jean-François Beauchef Dec 17 '13 at 20:55
  • +1 because of correct java path for windows, I've been struggled with that. thanks! – Ruwanka De Silva Jul 19 '14 at 07:49