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
Adding general assetic cookbook article #459
Merged
weaverryan
merged 2 commits into
symfony:master
from
richardmiller-zz:adding_assetic_article
Jun 27, 2011
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
How to Use Assetic for Asset Management | ||
======================================= | ||
|
||
Assetic is an asset management library which is packaged with the standard | ||
Symfony2 distribution, it has a bundle to allow it to be easily used | ||
in Symfony2 directly from Twig or PHP templates. It works with assets and | ||
filters. The assets are files such as CSS, JavaScript and images files. | ||
There are various filters that can be applied to these files before they | ||
are served to the browser. This allows a separation between the asset files | ||
stored in the application and the files actually presented to the user. | ||
Without using Assetic or another asset manager you are just directly serving | ||
up the files that are stored in the application: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: html+jinja | ||
|
||
<script src="{{ asset('js/script.js') }}" type="text/javascript" /> | ||
|
||
.. code-block:: php | ||
|
||
<script src="<?php echo $view['assets']->getUrl('js/script.js') ?>" | ||
type="text/javascript" /> | ||
|
||
Assets | ||
------ | ||
|
||
Using Assetic provides many advantages over directly serving the files. | ||
The files do not need to be stored where they are served from and can be | ||
drawn from various sources such as within a bundle: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: html+jinja | ||
|
||
{% javascripts '@AcmeFooBundle/Resources/public/js/*' | ||
%} | ||
<script src="{{ asset_url }}"></script> | ||
{% endjavascripts %} | ||
|
||
.. code-block:: html+php | ||
|
||
<?php foreach ($view['assetic']->javascripts( | ||
array('@AcmeFooBundle/Resources/public/js/*')) as $url): ?> | ||
<script src="<?php echo $view->escape($url) ?>"></script> | ||
<?php endforeach; ?> | ||
|
||
You can also combine several files into one. This helps to reduce the number | ||
of HTTP requests which is good for front end performance, as most browsers | ||
will only process a limited number at a time slowing down page load times. | ||
It also allows you to maintain the files more easily by splitting them into | ||
manageable parts. This can also help with re-usability as you can easily | ||
split project specific files from those which can be used in other applications | ||
but still serve them as a single file: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: html+jinja | ||
|
||
{% javascripts '@AcmeFooBundle/Resources/public/js/*' | ||
'@AcmeBarBundle/Resources/public/js/form.js' | ||
'@AcmeBarBundle/Resources/public/js/calendar.js' | ||
%} | ||
<script src="{{ asset_url }}"></script> | ||
{% endjavascripts %} | ||
|
||
.. code-block:: html+php | ||
|
||
<?php foreach ($view['assetic']->javascripts( | ||
array('@AcmeFooBundle/Resources/public/js/*', | ||
'@AcmeBarBundle/Resources/public/js/form.js', | ||
'@AcmeBarBundle/Resources/public/js/calendar.js')) as $url): ?> | ||
<script src="<?php echo $view->escape($url) ?>"></script> | ||
<?php endforeach; ?> | ||
|
||
|
||
This does not only apply to your own files you can also use Assetic to | ||
combine third party assets, such as jQuery with your own into a single file: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: html+jinja | ||
|
||
{% javascripts '@AcmeFooBundle/Resources/public/js/thirdparty/jquery.js' | ||
'@AcmeFooBundle/Resources/public/js/*' | ||
%} | ||
<script src="{{ asset_url }}"></script> | ||
{% endjavascripts %} | ||
|
||
.. code-block:: html+php | ||
|
||
<?php foreach ($view['assetic']->javascripts( | ||
array('@AcmeFooBundle/Resources/public/js/thirdparty/jquery.js', | ||
'@AcmeFooBundle/Resources/public/js/*')) as $url): ?> | ||
<script src="<?php echo $view->escape($url) ?>"></script> | ||
<?php endforeach; ?> | ||
|
||
Filters | ||
------- | ||
|
||
Additionally to this Assetic can apply filters to the assets before they | ||
are served. This includes tasks such as compressing the output for smaller | ||
file sizes which is another valuable front end optimisation. Other filters | ||
include compiling JavaScript file from CoffeeScript files and SASS to CSS. | ||
|
||
Many of the filters do not do the work directly but use other libraries | ||
to do it, this so you will often have to install that software as well. | ||
The great advantage of using Assetic to invoke these libraries is that | ||
instead of having to run them manually when you have worked on the files, | ||
Assetic will take care of this for you and remove this step altogether | ||
from your development and deployment processes. | ||
|
||
To use a filter you must specify it in the Assetic configuration | ||
as they are not enabled by default. For example to use the JavaScript YUI | ||
Compressor the following config needs to be added: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/config.yml | ||
assetic: | ||
filters: | ||
yui_js: | ||
jar: "%kernel.root_dir%/Resources/java/yuicompressor.jar" | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/config.xml --> | ||
<assetic:config> | ||
<assetic:filter | ||
name="yui_js" | ||
jar="%kernel.root_dir%/Resources/java/yuicompressor.jar" /> | ||
</assetic:config> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/config.php | ||
$container->loadFromExtension('assetic', array( | ||
'filters' => array( | ||
'yui_js' => array( | ||
'jar' => '%kernel.root_dir%/Resources/java/yuicompressor.jar', | ||
), | ||
), | ||
)); | ||
|
||
|
||
You can then specify using the filter in the template: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: html+jinja | ||
|
||
{% javascripts '@AcmeFooBundle/Resources/public/js/*' filter='yui_js' %} | ||
<script src="{{ asset_url }}"></script> | ||
{% endjavascripts %} | ||
|
||
.. code-block:: html+php | ||
|
||
<?php foreach ($view['assetic']->javascripts( | ||
array('@AcmeFooBundle/Resources/public/js/*'), | ||
array('yui_js')) as $url): ?> | ||
<script src="<?php echo $view->escape($url) ?>"></script> | ||
<?php endforeach; ?> | ||
|
||
|
||
A more detail guide to configuring and using Assetic filters as well as | ||
details of Assetic's debug mode can be found in :doc:`/cookbook/assetic/yuicompressor`. | ||
|
||
Controlling the URL used | ||
------------------------ | ||
|
||
If you wish to you can control the URLs which Assetic produces. This is | ||
done from the template and is relative to the public document root: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: html+jinja | ||
|
||
{% javascripts '@AcmeFooBundle/Resources/public/js/*' | ||
output='js/combined.js' | ||
%} | ||
<script src="{{ asset_url }}"></script> | ||
{% endjavascripts %} | ||
|
||
.. code-block:: html+php | ||
|
||
<?php foreach ($view['assetic']->javascripts( | ||
array('@AcmeFooBundle/Resources/public/js/*'), | ||
array(), | ||
array('output' => 'js/combined.js') | ||
) as $url): ?> | ||
<script src="<?php echo $view->escape($url) ?>"></script> | ||
<?php endforeach; ?> | ||
|
||
Caching the output | ||
------------------ | ||
|
||
The process of creating the files served up can be quite slow especially | ||
when using some of the filters which invoke third party software to the | ||
actual work. Even when working in the development environment the slow | ||
down in the page loads if this was to be done each time would quickly get | ||
frustrating. Fortunately in the dev environment Assetic caches the output | ||
so this will not happen, rather than having to clear the cache manually | ||
though, it monitors for changes to the assets and regenerates the files | ||
as needed. This means you can work on the asset files and see the results | ||
on page load but without having to suffer continual slow page loads. | ||
|
||
For production, where you will not be making changes to the asset files, | ||
performance can be increased by avoiding the step of checking for changes. | ||
Assetic allows you to go further than this and avoid touching Symfony2 | ||
and even PHP at all when serving the files. This is done by dumping all | ||
of the output files using a console command. These can then be served directly | ||
by the web server as static files, increasing performance and allowing the | ||
web server to deal with caching headers. The console command to dump the files | ||
is: | ||
|
||
.. code-block:: bash | ||
|
||
php app/console assetic:dump | ||
|
||
.. note:: | ||
|
||
Once you have dumped the output this you will need to run the console | ||
command again to see any new changes. If you run it on your development | ||
server you will need to remove the files in order to start letting Assetic | ||
process the assets on the fly again. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the is an extra
this
here