4

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?

Community
  • 1
  • 1
blamb
  • 4,220
  • 4
  • 32
  • 50
  • i may have commented on that post, but my reputation is messed up. i dont feel this is deserving. anybody hvae any to share so i can comment and answer questions which im currently banned from? – blamb Mar 19 '14 at 22:18
  • 2
    I could be wrong but the parts are only used when in the `dev` environment. Are you accessing the site using `app_dev.php`? and if so do you still have the `part_` files when accessing using `app.php`? – Chase Mar 20 '14 at 00:43
  • i was in dev env , yes. you know im not sure if i was seeing them on app.php now. It only happens when i use the assetic naming to call the files '@our_custom_js' so i left that config in assetic: for compiling reasons, but call it by name only now just so its named correctly `bundles/mybundle/js/base.js`. Even if its only happening in dev, its still cryptic, and bothersome that theres no control over it, until someone finds the config magic to overcome it. I spent all day on assetic config getting to the bottom of filter problems, and naming problems. – blamb Mar 20 '14 at 21:11

1 Answers1

1

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.

Community
  • 1
  • 1
Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97