Question

When i compile my code using assetic my js files are being named part_1.js, part_2.js. i dont see this referent to part_ anywhere in my code. where is this coming from?

config.yml

assetic:
     assets:
         our_custom_js:
            inputs:
                - '@MyBundle/Resources/public/js/base.js'
            filters:    []
            output: 'custom.js'
         fos_js_routes:
             inputs:
                 - 'bundles/fosjsrouting/js/router.js'
             output: 'fos_js_router.js'`    

base.html.twig

{% javascripts combine=false output="sandbox.js"
    '@our_custom_js'
    '@fos_js_routes'        
%}
    <script src="{{ asset_url }}"></script>
    <script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>

{% endjavascripts %}

my source ends up looking like this

<script src="/sandbox_part_1.js"></script>
<script src="/js/routing?callback=fos.Router.setData"></script>
<script src="/sandbox_part_2.js"></script>
<script src="/js/routing?callback=fos.Router.setData"></script>

This question was also asked here How to make Symfony 2 asset compilation to product different filenames?

Was it helpful?

Solution

Just answered the same question here. The 'part_#' string is added when accessing your application in the development environment (app_dev.php).

By default, {% javascripts %} will print all your assets/scripts, using a <script> line for each script. In the production environment, they are combined.

The {% javascripts %} function acts like a foreach loop in dev environment, while it will combine all assets in to a single <script> line in production. If you take a look at the PHP-script in the documentation, you can see that it's using foreach:

<?php foreach ($view['assetic']->javascripts(
    array(
        '@AppBundle/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 ?>

Your base.html.twig should look like this:

{% javascripts combine=false output="sandbox.js"
    '@our_custom_js'
    '@fos_js_routes'        
%}
    <script src="{{ asset_url }}"></script>
{% endjavascripts %}
    <script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>

When the other <script> is not inside the javascripts part, it will not be in the loop, so it will be printed only once.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top