Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Select2 Bundle Laravel Package

pinano/select2-bundle

Symfony2 bundle that packages Select2 (v4.0.3) assets for easy use in Twig/Assetic. Install via Composer, register the bundle, run assets:install, then include select2 JS/CSS plus optional i18n locale files.

View on GitHub
Deep Wiki
Context7

Select2 Bundle for Symfony2

Current Version

Select2 4.0.3

Installation

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

Usage

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="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    {% javascripts
        ...
        '@PinanoSelect2Bundle/Resources/public/js/select2.min.js'
        '@PinanoSelect2Bundle/Resources/public/js/i18n/es.js'
        ...
        %}
        <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'
      ...
      'bundles/pinanoselect2/css/select2.css'
      ...
    %}
        <link rel="stylesheet" href="{{ asset_url }}" />
    {% endstylesheets %}
{% endblock %}

Note: See https://github.com/kriswallsmith/assetic/issues/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.

Licenses

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.

References

  1. https://select2.github.io/
  2. http://symfony.com
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor