1

I'm upgrading my code from symfony2.6 to symfony2.7 and run into a weird thing with my assets.

example codes are a bit shortened.

I'm using this to set my css files in a head.html.twig.

{% stylesheets '@MyAwesomeBundle/Resources/subfolder*.less' %}
<link rel="stylesheet" href="{{ asset_url }}"/>
{% endstylesheets %}

This in my config file:

# Assetic Configuration
assetic:
    debug:          %kernel.debug%
    bundles:
      - MyAwesomeBundle
    node:       %path.nodejs%
    node_paths: [ %path.node_modules% ]
    filters:
        less:
            apply_to:   "\.less$"

The less files are compiled fine. The problem is the resulting url in the webpage, which looks like this:

<link rel="stylesheet" href="\2F css\2F 36f682f\5F bootstrap\5F 1\2E css"/>

When I add a |raw filter to the twig-file the path is just fine.

{# twig #}
<link rel="stylesheet" href="{{ asset_url|raw }}"/>

{# resulting html #}
<link rel="stylesheet" href="/css/36f682f_bootstrap_1.css"/>

Any idea how to remedy this? I hate to go through all my twig files and add the raw-filter.

I've looked at all dependencies I'm pulling in with composer, they appear to be at the latest stable versions (no dev-versions being used).

2 Answers 2

1

symfony2.7 parse files differently, depending on the file extension (or name).

Variable "asset_url" is secured (and no escaped) only in .html.twig files. In other case (for examle: head.js.twig, head_js.twig etc. ), it will escaped and, accordingly, require "| raw" filter.

1
  • Thank you, this was the solution for me. I had files with names like "thing.css.twig" and it was broken. Renaming to "thing.css.html.twig" fixed the problem!
    – nobane
    Jan 25, 2017 at 3:29
0

I'm still unsure on why I ran into this issue, but I've fixed my problem with a small change in my base templates.

I have a block called "head" in my base templates. Which included 2 external templates for the default css and js files (which should always be included). When I moved the contents of these templates into the base template my problem went away. The problem was only present for those css+js files. In other places the problem was not present.

It might also be related to calling the parent() template, which would be done to add aditional css or javascript on specific pages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.