Skip to content

Assetic fixes #1991

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

Closed
wants to merge 3 commits 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
49 changes: 48 additions & 1 deletion cookbook/assetic/asset_management.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ drawn from various sources such as from within a bundle:
<link rel="stylesheet" href="<?php echo $view->escape($url) ?>" />
<?php endforeach; ?>

.. tip::

For the above example to work, you must also add your bundle to the list
of bundles that Assetic handles, which is empty by default:

.. code-block:: yaml

# app/config/config.yml
assetic:
debug: "%kernel.debug%"
Copy link
Member

Choose a reason for hiding this comment

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

%kernel.debug% is the default value for the assetic.debug setting, so I think this line isn't needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's in the config like this in the standard distribution though so its in keeping with that

Copy link
Member

Choose a reason for hiding this comment

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

Small detail either way, but what I'd love the most is to use the # ... in this spot:

assetic:
    # ...
    bundles:    [ AcmeFooBundle ]
    filters:
        # ...

Also, is this new note actually correct yet? My impression is that you don't need to add your bundle to the bundles configuration until you place a stylesheets or javascripts tag into a template that lives inside that bundle. If your stylesheets or javascripts tags are only in a template in app/Resources/views (even if you're referencing CSS/JS files that are in some bundle), then you don't need to add the bundle to the bundles key.

If I'm wrong, some please shout at me! :) I just wanted to make sure we were clear on that before merging.

Thanks!

Copy link
Author

Choose a reason for hiding this comment

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

My impression is that you don't need to add your bundle to the bundles configuration until you place a stylesheets or javascripts tag into a template that lives inside that bundle.

I strongly suspect that you are correct. It makes a lot more sense to me now. I’ll make some tests next week and rewrite accordingly.

use_controller: false
bundles: [ AcmeFooBundle ]
Copy link
Member

Choose a reason for hiding this comment

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

This should be [AcmeFooBundle]

filters:
# ...

In this example, all of the files in the ``Resources/public/js/`` directory
of the ``AcmeFooBundle`` will be loaded and served from a different location.
The actual rendered tag might simply look like:
Expand All @@ -87,10 +102,42 @@ The actual rendered tag might simply look like:
.. note::

This is a key point: once you let Assetic handle your assets, the files are
served from a different location. This *can* cause problems with CSS files
served from a different location. This *will* cause problems with CSS files
that reference images by their relative path. However, this can be fixed
by using the ``cssrewrite`` filter, which updates paths in CSS files
to reflect their new location.

Unfortunately, the ``cssrewrite`` filter does not work when using the
``@AcmeFooBundle`` syntax to reference the assets. This is a known
limitation. A workaround is to use the ``output`` option to change the
location the files are served from to a path from which the relative paths
work, e.g.:

.. configuration-block::

.. code-block:: html+jinja

{% stylesheets
output='bundles/acmefoo/css/compiled.css'
'@AcmeFooBundle/Resources/public/css/*'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}

.. code-block:: html+php

<?php foreach ($view['assetic']->stylesheets(
array('@AcmeFooBundle/Resources/public/css/*'),
array(),
array('output' => 'bundles/acmefoo/css/compiled.css')
) as $url): ?>
<link rel="stylesheet" href="<?php echo $view->escape($url) ?>" />
<?php endforeach; ?>

Of course, this assumes that all the CSS files you serve in this block use
relative paths that start from the same location, which is not always
convenient. This is the reason this is called a *workaround* and not a
*solution*.
Copy link
Member

Choose a reason for hiding this comment

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

I actually didn't know that you could work around this problem by changing the output to a path in the bundle. It's interesting, but I think that we should just tell people to not use the @MyBundle shortcut at all, mention the problem with cssrewrite and that's it. Your timing is actually pretty awesome, I had just changed the stylesheets example in this doc yesterday to not use the @MyBundle syntax.

So, can you rebase against the latest changes, then simple mention the problem with using the @MyBundle shortcut syntax? I just want to show people the right way of doing it, and make sure they know quickly when the other way will cause them issues.

Thanks!

Copy link
Author

Choose a reason for hiding this comment

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

No problem, I’ll do that.


Combining Assets
~~~~~~~~~~~~~~~~
Expand Down