Symfony2 bundle for wrapping the famous select2 jquery plugin by @ivaynberg

Installs: 190 663

Dependents: 4

Suggesters: 0

Security: 0

Stars: 16

Watchers: 1

Forks: 9

Open Issues: 2



v4.0.3 2016-11-15 09:47 UTC

This package is not auto-updated.

Last update: 2024-05-11 12:39:26 UTC


Current Version

Select2 4.0.3


Add bundle to your composer.json file

// composer.json

    "require": {
        // ...
        "pinano/select2-bundle": "dev-master"

Or, if you prefer, choose a specific version

// composer.json

    "require": {
        // ...
        "pinano/select2-bundle": "4.0.3"

Add bundle to your application kernel

// app/AppKernel.php

public function registerBundles()
    $bundles = array(
        // ...
        new Pinano\Select2Bundle\PinanoSelect2Bundle(),
        // ...

Download the bundle using Composer

$ php composer.phar update pinano/select2-bundle

Install assets

Given your server's public directory is named "web", install the public vendor resources

$ php app/console assets:install web

Optionally, use the --symlink attribute to create links rather than copies of the resources

$ php app/console assets:install --symlink web


Once all the resources are in place you can edit any of your twig views or layouts to include the Select2 javascript files.

Select2 optionally supports multiple languages by simply including the right language javascript file (i18n/es.js, i18n/fr.js, etc.) after select2.js. In the following example we are loading the Spanish locale.

Note: Select2 requires the jQuery library.

{% block javascripts %}
    <script src="//"></script>
    {% javascripts
        <script src="{{ asset_url }}"></script>
    {% endjavascripts %}
{% endblock %}

Then you will want to load the css resources so your select elements look nice:

{% block stylesheets %}
    {% stylesheets filter='cssrewrite'
        <link rel="stylesheet" href="{{ asset_url }}" />
    {% endstylesheets %}
{% endblock %}

Note: See kriswallsmith/assetic#53 for known limitations of assetic with CSS referencing.

I usually follow a simple inheritance schema when it comes to designing twig templates. That is, I have an app/Resources/views/base.html.twig file that I use as a site-wide template. Then, inside every bundle I have my own Resources/views/layout.html.twig file that extends the base template. Depending on the kind of application I'm designing I can place the Bootstrap stuff in the site-wide or the bundle-wide template. Then every view of a given bundle will extend the corresponding bundle layout.html.twig file, which in turn extends the site-wide template.

The folks at Sensio Labs have already covered this approach and you can check it in their documentation.


I do not own Select2 files at all, I'm just providing a Bundle package to easy-install them all. Refer to the source code of the included files from Select2 for license information.