Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a section about using named assets #3496

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions cookbook/assetic/asset_management.rst
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,79 @@ combine third party assets, such as jQuery, with your own into a single file:
<script src="<?php echo $view->escape($url) ?>"></script>
<?php endforeach; ?>

Using Named Assets
~~~~~~~~~~~~~~~~~~

AsseticBundle configuration directives allow you to define named asset sets.
You can do so by defining the input files, filters and output files in your
configuration under the ``assetic`` section. Read more in the
:doc:`assetic config reference </reference/configuration/assetic>`.

.. configuration-block::

.. code-block:: yaml

# app/config/config.yml
assetic:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a file comment as the first line of the code block: # app/config/config.yml

assets:
jquery_and_ui:
inputs:
- '@AcmeFooBundle/Resources/public/js/thirdparty/jquery.js'
- '@AcmeFooBundle/Resources/public/js/thirdparty/jquery.ui.js'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XML:

<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:assetic="http://symfony.com/schema/dic/assetic">

    <assetic:config>
        <assetic:asset name="jquery_and_ui">
            <assetic:input>@AcmeFooBundle/Resources/public/js/thirdparty/jquery.js</assetic:input>
            <assetic:input>@AcmeFooBundle/Resources/public/js/thirdparty/jquery.ui.js</assetic:input>
        </assetic:asset>
    </assetic:config>
</container>

PHP:

// app/config/config.php
$container->loadFromExtension('assetic', array(
    'assets' => array(
        'jquery_and_ui' => array(
            'inputs' => array(
                '@AcmeFooBundle/Resources/public/js/thirdparty/jquery.js',
                '@AcmeFooBundle/Resources/public/js/thirdparty/jquery.ui.js',
            ),
        ),
    ),
);


.. code-block:: xml

<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:assetic="http://symfony.com/schema/dic/assetic">

<assetic:config>
<assetic:asset name="jquery_and_ui">
<assetic:input>@AcmeFooBundle/Resources/public/js/thirdparty/jquery.js</assetic:input>
<assetic:input>@AcmeFooBundle/Resources/public/js/thirdparty/jquery.ui.js</assetic:input>
</assetic:asset>
</assetic:config>
</container>

.. code-block:: php

// app/config/config.php
$container->loadFromExtension('assetic', array(
'assets' => array(
'jquery_and_ui' => array(
'inputs' => array(
'@AcmeFooBundle/Resources/public/js/thirdparty/jquery.js',
'@AcmeFooBundle/Resources/public/js/thirdparty/jquery.ui.js',
),
),
),
);

After you have defined the named assets, you can reference them in your templates
with the ``@named_asset`` notation:

.. configuration-block::

.. code-block:: html+jinja

{% javascripts
'@jquery_and_ui'
'@AcmeFooBundle/Resources/public/js/*' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}

.. code-block:: html+php

<?php foreach ($view['assetic']->javascripts(
array(
'@jquery_and_ui',
'@AcmeFooBundle/Resources/public/js/*',
)
) as $url): ?>
<script src="<?php echo $view->escape($url) ?>"></script>
<?php endforeach; ?>

.. _cookbook-assetic-filters:

Filters
Expand Down