Karolis
Karolis

Reputation: 2618

symfony inline asset with assetic

I have the following code based on Symfony2 docs:

    {% block javascripts %}

        {% javascripts filter="?jsqueeze" output="js/app.js"
            "%kernel.root_dir%/Resources/assets/js/one.js"
            "%kernel.root_dir%/Resources/assets/js/two.js" %}
            <script src="{{ asset_url }}"></script>
        {% endjavascripts %}

    {% endblock %}

which works like charm.

What I want to do, is instead of

<script src="{{ asset_url }}"></script>

use something like:

<script> ... inline asset contents here ... </script>

I tried assetic docs as well as digging around AsseticTokenParser and AsseticNode classes, but couldn't find a variable which references to the actual filepath instead of url.

Normally I do it with css rather than js, but it should be the same concept.

thanks!

Upvotes: 4

Views: 1302

Answers (1)

Karolis
Karolis

Reputation: 2618

In production mode, symfony generates a link to a static file, which can be included directly into the template. This file needs to be generated with something like:

php app/console assetic:dump --env=prod --no-debug

In dev mode, there is no static file generated which can be included. The asset_url contains an url which will call symfony controller which will in turn generate required js/css files on the fly. Aparently it is very easy to include the output of a controller in symfony:

{% render asset_url %}

To answer my own question, one could write a twig extension, which would include a static file or forward request to render method depending on wether it has been called in prod or dev mode.

I will update this page if/when I write an extension like this.

Another option is not to use dev mode of assetic and always generate static css/js files on the fly with:

php app/console assetic:watch

Then a simple include would work.

I wish there was an out of the box method in Symfony to get this common task done.

Upvotes: 2

Related Questions