user1098965
user1098965

Reputation:

How to combine these assetics in Symfony2?

I'm using ExposeTranslationBundle (expose translations to javascript) and JMSI18nRoutingBundle (expose routes to javascript). This is part of my <head> tag:

    {% javascripts filter='?yui_js' output='js/app.js'
        '../app/Resources/public/js/jquery-*.js'
        '../app/Resources/public/js/jquery/*'
        '../app/Resources/public/js/app.js'
        'bundles/fosjsrouting/js/router.js'
        'bundles/bazingaexposetranslation/js/translation.js' %}
        <script src="{{ asset_url }}" ></script>
    {% endjavascripts %}

    <!-- ExposeTranslationBundle and JMSI18nRoutingBundle -->
    <script src="{{ path('fos_js_routing_js',
        {"callback": "fos.Router.setData"}) }}"></script>
    <script src="{{ url('bazinga_exposetranslation_js') }}"></script>

Is possible to combine the last two <script> imports into first assetic and how?

Upvotes: 5

Views: 1356

Answers (2)

dvdvck
dvdvck

Reputation: 435

Instead of import, you could happily put it inline, ie:

<script type="text/javascript">
{# BazingaExposeTranslation #}
{% render 'bazinga.exposetranslation.controller:exposeTranslationAction' 
   with { domain_name: "messages", _locale:app.session.locale, _format: "js" } %}

{# JMSI18nRoutingBundle ... #}

</script>

You need to check the routing file for those bundles.

Upvotes: 1

room13
room13

Reputation: 1922

I thing it is not possible because the FOSJSRouting javascript file is generated by a controller. Internaly the bundles caches the js but in app/cache, so it needs to go through the controller every request. I'm not familiar with the expose translation bundle but I guess it's the same problem here.

There has been an ongoing discussion in the issue tracke of FOSJsRouterBundle on github and there is also a sollution. See the full issue here: https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/issues/22

The workaround is to have a script or command to dump the output to files in web/js directory:

<?php

require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';

use Symfony\Component\HttpFoundation\Request;

$kernel = new AppKernel('stage', false);
$kernel->loadClassCache();
$response = $kernel->handle(Request::create('/js/routing?callback=fos.Router.setData'));

file_put_contents(__DIR__.'/../web/js/routes.js', $response->getContent());

This is somewhat of a workaround sollution. I've been thinking about implementing a generic bundle wich whome this task can be configured for several other bundles using controllers to output js. The controller actions would have to be configured in a yml file and then a command would have have to be executed at each deployment/modification of routes/strings. But I havn't had time for this... yet ;)

Upvotes: 2

Related Questions