Work with bootstrap, assetic and less

Bootstrap is a well known and powerful front-end framework for fast prototyping, it uses LESS and it can be easily integrate in your Symfony applications with the help of assetic. In this tutorial I’ll show how to:

  • Install bootstrap in you Symfony application
  • Load it using assetic
  • Compile bootstrap LESS files with lessphp

Let’s start with a fresh Symfony2.1 application, the best way to create a new application is using composer:

composer create-project symfony/framework-standard-edition bootstrapdemo 2.1.x-dev

After that, we need to install bootstrap within our application, again we will use composer to download and install the source code. Bootstrap doesn’t have a composer package so we’ll need a little bit of extra work to get it done.

Add the repository definition to your composer.json:

"repositories": [
            "type": "package",
            "package": {
                "name": "twitter/bootstrap",
                "version": "2.1.0",
                "source": {
                    "type": "git",
                    "url": "",
                    "reference": "v2.1.0"

and then, add bootstrap as a dependency as usual:

"twitter/bootstrap": "v2.1.0"

Mind that we are not installing the latest bootstrap version, but 2.1.0 because of a known issue with lessphp. By the time you read, this could be solved, you should try using “master” instead of “2.1.0″ and see how it goes

Last but not least, we need lessphp in order to be able to compile our boostrap .less files, let’s add to our composer.json:

"leafo/lessphp": "dev-master"

Now we can run:

composer update twitter/bootstrap leafo/lessphp

This will download needed sources. Fair enough, it’s time to start writing some Symfony code, isn’t it?

Assets management with assetic is well documented in this cookbook, if you are completely new to it I’ll suggest to start from there.

Assetic ships whit a lot of useful filters, we’ll use LessphpFilter, so we need to configure it (some filters require configuration params, some don’t) in config.yml, find assetic section and add:

            file: %kernel.root_dir%/../vendor/leafo/lessphp/
            apply_to: ".less$"

This will define lessphp path, and will apply the filter to all our .less files. This filter is mandatory for less files, so I usually apply it to all .less files by default. After that, the last thing we need is to include bootstrap in main template:

{% stylesheets '%kernel.root_dir%/../vendor/twitter/bootstrap/less/bootstrap.less' %}

{% endstylesheets %}

Nothing special here, just mind that we are using absolute path for boostrap file, this allows us to work with vendor files, and that we are not specifying lessphp filter since it will be applied by default for .less files.

And that’s all.

Assetic is a really powerful tool and it can really improve the way you manage and optimize your assets, we are planning to release a premium screencast about advanced assetic usage with twig, if you think you’ll love it give us some feedback and let us know what you should see in it.

Happy coding.

Tags: , , , , ,

18 Responses to “Work with bootstrap, assetic and less”

  1. Chris 04/10/2012 at 12:54 PM #

    I spent four hours yesterday trying (and failing) to get the MOPA bootstrap bundle working. Then I created a new project, and within 5 mins had bootstrap working perfectly thanks to your instructions.

    Great post, thanks so much for sharing – I’m no designer and so bootstrap is a godsend for me.

  2. Darren Black 16/10/2012 at 4:09 AM #

    Yeah, thanks for this – rolling back to v2.1.0 and the absolute path for the bootstrap files was all i was missing.

  3. Dan Ekström 04/11/2012 at 2:05 PM #


    I have a problem that I can not use the imports % stylesheets, I get an error message about including the bundle in the assetic file??

    I would just like to get started using bootstrap, I don’t have any special requirements.

    • ftassi 04/11/2012 at 10:38 PM #

      You need to whitelist the bundles that are allowed to use assetic. You should have a bundles key under assetic configuration in config.yml. You can add your bundles to that list or just remove the key if you want to enable assetic for all bundles.

  4. Piotr Belina 18/11/2012 at 8:23 PM #


    The element is missing in the last snippet. I modified it like this:

    {% stylesheets ‘%kernel.root_dir%/../vendor/twitter/bootstrap/less/bootstrap.less’ %}

    {% endstylesheets %}

  5. Kathrine 09/12/2012 at 1:55 PM #

    I spent like million hours trying to get Mopa to work and all for nothing. Your tutorial made it work in couple of minutes. I absolutely love it!
    If I just can make little remark: how about including this line in the tutorial? Makes it all ready to go.
    {% stylesheets
    <link rel="stylesheet" href="{{ asset_url }}" /
    {% endstylesheets %}

    • ftassi 08/06/2013 at 11:45 AM #

      Done, thank you.

      • ZipZit 19/06/2013 at 9:06 AM #

        Whoa.. I’ve been spending many many hours trying to make this thing work. For anyone else who may get here… I placed the stylesheet note into my bundle’s layout.html.twig file. Got lots of misleading errors. Turns out you got to place the stylesheet INSIDE the header, not BEFORE it… (well duh!)

        Thank you for posting up the tutorial in the first place.

  6. Ben Overmyer 15/01/2013 at 7:09 PM #

    You are my hero. This worked effortlessly!

  7. Francesc Rosàs 17/01/2013 at 11:14 PM #

    Bootstrap is now in Packagist so we can simply do:

    “leafo/lessphp”: “~0.3″,
    “twitter/bootstrap”: “~2.2″

    in composer.json. Also, specificying the lessphp file is not (longer?) required:

    apply_to: “.less$”

    • Andrew 04/02/2013 at 8:38 PM #

      yea, no filters necessary over here…tossed an exception with the filters in the config.yml file

  8. Francesc Rosàs 18/01/2013 at 10:07 AM #

    I’ve found a pair of inconveniences:

    If you want to use one of the Bootstrap variables (like @baseFontSize) in another file you can’t do this:

    {% stylesheets ‘bootstrap.less’ 'main.less' %}

    The solution I came up is to import them from a same file:

    /* imports.less */
    @import "bootstrap.less";
    @import "main.less";

    and load this file then:

    {% stylesheets 'imports.less' %}

    It is worst if you want to change these Bootstrap variables. As they are defined in the variables.less referenced from bootstrap.less, one have to either use a custom bootstrap.less or modify the original bootstrap.less like this:

    @import "variables.less"; // Modify this for custom colors, font-sizes, etc
    @import "../../../css/bootstrap-variables.less";

    but then you can’t use Composer to install Bootstrap for you.

    Not sure which is the less ugly solution.

    PD: Allowing the use of pre tag in the comments would be great :)

    • Error500 13/04/2013 at 8:40 PM #


      did you find a solution for this problem ?
      another way could be to add a line at the end of variables.less to import the project’s variable file ?

  9. Wow! Thank you! I permanently needed to write on my site something like that. Can I take a part of your post to my website?

    • ftassi 19/06/2013 at 9:22 AM #

      Feel free to use this post as you want. We would appreciate a link to the original content though

  10. George 13/08/2013 at 4:56 AM #

    Video Tutorials for Symfony with Twitter Bootstrap..Project files available for download

  11. jm 16/08/2013 at 9:59 PM #

    Your copy and paste trick is especially annoying with source code!

  12. balance transfer credit cards 0 apr 09/10/2013 at 1:03 AM #

    Hey, I think your blog might be having browser compatibility issues.
    When I look at your blog in Chrome, it looks fine but when opening in Internet
    Explorer, it has some overlapping. I just wanted to give you
    a quick heads up! Other then that, amazing blog!

Leave a Reply