8000 Add assetic DIC tags reference. by pvolok · Pull Request #2480 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Add assetic DIC tags reference. #2480

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

Merged
merged 1 commit into from
May 3, 2013
Merged
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
171 changes: 171 additions & 0 deletions reference/dic_tags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ the AsseticBundle has several tags that aren't listed here.
+-----------------------------------+---------------------------------------------------------------------------+
| Tag Name | Usage |
+-----------------------------------+---------------------------------------------------------------------------+
| `assetic.asset`_ | Register an asset to the current asset manager |
+-----------------------------------+---------------------------------------------------------------------------+
| `assetic.factory_worker`_ | Add a factory worker |
+-----------------------------------+---------------------------------------------------------------------------+
| `assetic.filter`_ | Register a filter |
+-----------------------------------+---------------------------------------------------------------------------+
| `assetic.formula_loader`_ | Add a formula loader to the current asset manager |
+-----------------------------------+---------------------------------------------------------------------------+
| `assetic.formula_resource`_ | Adds a resource to the current asset manager |
+-----------------------------------+---------------------------------------------------------------------------+
| `assetic.templating.php`_ | Remove this service if php templating is disabled |
+-----------------------------------+---------------------------------------------------------------------------+
| `assetic.templating.twig`_ | Remove this service if twig templating is disabled |
+-----------------------------------+---------------------------------------------------------------------------+
| `data_collector`_ | Create a class that collects custom data for the profiler |
+-----------------------------------+---------------------------------------------------------------------------+
| `form.type`_ | Create a custom form field type |
Expand Down Expand Up @@ -53,6 +67,163 @@ the AsseticBundle has several tags that aren't listed here.
| `validator.initializer`_ | Register a service that initializes objects before validation |
+-----------------------------------+---------------------------------------------------------------------------+

assetic.asset
-------------

**Purpose**: Register an asset to the current asset manager

assetic.factory_worker
----------------------

**Purpose**: Add a factory worker

Factory worker is a class implementing
``Assetic\\Factory\\Worker\\WorkerInterface``. Its ``process($asset)``
method is called for each asset after asset creation. You can modify an asset or
even return a new one.

In order to add a new worker, first create a class::

use Assetic\Asset\AssetInterface;
use Assetic\Factory\Worker\WorkerInterface;

class MyWorker implements WorkerInterface
{
public function process(AssetInterface $asset)
{
// ... change $asset or return a new one
}

}

And then add register it as a tagged service:

.. configuration-block::

.. code-block:: yaml

services:
acme.my_worker:
class: MyWorker
tags:
- { name: assetic.factory_worker }

.. code-block:: xml

<service id="acme.my_worker" class="MyWorker>
<tag name="assetic.factory_worker" />
</service>

.. code-block:: php

$container
->register('acme.my_worker', 'MyWorker')
->addTag('assetic.factory_worker')
;

assetic.filter
--------------

**Purpose**: Register a filter

AsseticBundle uses this filter to register common filters. You can also use this
tag to register your own filters.

First, you need to create a filter::

use Assetic\Asset\AssetInterface;
use Assetic\Filter\FilterInterface;

class MyFilter implements FilterInterface
{
public function filterLoad(AssetInterface $asset)
{
$asset->setContent('alert("yo");' . $asset->getContent());
}

public function filterDump(AssetInterface $asset)
{
Copy link
Member

Choose a reason for hiding this comment

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

do you really mean an empty function, or do you mean // ...?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Usually filters do their job in one of these methods, while the second one is empty. Should I still add // ... into the second?

// ...
}
}

Second, define a service:

.. configuration-block::

.. code-block:: yaml

services:
acme.my_filter:
class: MyFilter
tags:
- { name: assetic.filter, alias: my_filter }

.. code-block:: xml

<service id="acme.my_filter" class="MyFilter">
<tag name="assetic.filter" alias="my_filter" />
</service>

.. code-block:: php

$container
->register('acme.my_filter', 'MyFilter')
->addTag('assetic.filter', array('alias' => 'my_filter'))
;

Finally, apply the filter:

.. code-block:: jinja

{% javascripts
'@AcmeBaseBundle/Resources/public/js/global.js'
filter='my_filter'
%}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}

You can also apply your filter via ``assetic.filters.my_filter.apply_to`` config
option as it's described here: :doc:`/cookbook/assetic/apply_to_option`. In
order to do that, you must define your filter service in separate xml config
file and put this file's path to ``assetic.filters.my_filter.resource``.

assetic.formula_loader
----------------------

**Purpose**: Add a formula loader to the current asset manager

Formula loader is a class implementing
``Assetic\\Factory\Loader\\FormulaLoaderInterface`` interface. This class
is responsible in loading assets from a particular kind of resources (for
instance, twig template). Assetic ships loaders for php and twig templates.

An ``alias`` attribute defines a name of the loader.

assetic.formula_resource
------------------------

**Purpose**: Adds a resource to the current asset manager

A resource is something formulae can be loaded from. For instance, twig
templates are resources.

assetic.templating.php
----------------------

**Purpose**: Remove this service if php templating is disabled

The tagged service will be removed from the container if
``framework.templating.engines`` config section does not contain php.

assetic.templating.twig
----------------------

**Purpose**: Remove this service if twig templating is disabled

The tagged service will be removed from the container if
``framework.templating.engines`` config section does not contain twig.

data_collector
--------------

Expand Down
0