ladela/require-js-bundle

RequireJS integration for Symfony2

Installs: 13

Dependents: 0

Stars: 2

Watchers: 2

Language: PHP

v1.0.0 2014-02-18 09:29 UTC

README

RequireJS integration for Symfony2 with multiple configurations support.

Configuration

parameters:
    # This setting is required if you are going to use Assetic.
    assetic.filter_manager.class: Ladela\RequireJSBundle\FilterManager
ladela_require_js:
    # There can be multiple configs, you choose which one to use
    # when initializing RequireJS inside a template.
    config1:
        # This section configures the <script> tag attributes and RequireJS configuration options.
        javascript:
            # The value of 'src' attribute.
            src:       //cdnjs.cloudflare.com/ajax/libs/require.js/2.1.9/require.min.js
            # The value of 'data-main' attribute,
            # if not provided then the attribute will not be added to the <script> tag.
            data_main: ~
            # RequireJS configuration options,
            # see http://requirejs.org/docs/api.html#config
            options:
                baseUrl: ~
                paths:
                    #alias: path
                shim:
                    #module:
                    #    deps: []
                    #    exports: value
        # This section configures r.js optimizer if you are going to use Assetic.
        optimizer:
            # Path to r.js script (also requires node.js to be installed).
            r_js:    r.js
            # Configuration options for r.js,
            # see http://requirejs.org/docs/optimization.html#options
            options:
                baseUrl: .
                # If left empty then paths from 'javascript' options will be used.
                paths:
                    #alias: path
                # If left empty then shim from 'javascript' options will be used.
                shim:
                    #module:
                    #    deps: []
                    #    exports: value

    # Another config and so on.
    config2:
        ...

Usage

Inside your Twig template:

{{ require_js_init('config1') }}

If you are using Assetic then you may want to use 'r_js' filter to combine the scripts with help of r.js optimizer:

{% javascripts filter='r_js:config1' '@AcmeDemoBundle/Resources/public/js/app/main.js' %}
    {{ require_js_init('config1', {'data_main' : asset_url}) }}
{% endjavascripts %}

Note that you must provide the same config name in 'filter' attribute as in 'require_js_init' function. Filter and config names are separated by a colon.