Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - How to set CSS path based on current user local without losing twig and filters functionality

I work a multi-localization project using Symfony2 , and i separate ( CSS/Images ) on different folder based on current user local.

Folder Structure Ex:

Resources\
    public\
        css\
            ar\
                home.css
            en\
                home.css

**Now i need the Assetic to render the correct home.CSS file based on current user local {ar | en} without loosing twig and filters functionality **

Example - This not work :

{% stylesheets 
    'bundles/atcopcore/css/{ app.request.locale }/home.css' filter='cssrewrite'
%}
<link rel="stylesheet" href="{{ asset_url }}" />

Note : I want to get advantage of css compine and this can't be done if i do the following :

<link rel="stylesheet" href="{{ asset('css/' ~ app.request.locale ~ '/home.css') }}" />

How i can do this ...

i found a link could be usefull , but i think there is more professional way to do this.

How to embed stylesheets with Assetic based on a value in the session

Please , Any suggestions ?

like image 542
SMSM Avatar asked Sep 07 '14 09:09

SMSM


1 Answers

Assetic provides an functionality for you problem.

Add this to your configuration

assetic:
   # more from assetic
   variables:
      locale: [de, en, it, es] # whatever

And in your twig file:

{% stylesheets  filter='cssrewrite' 'bundles/atcopcore/css/home.{locale}.css' %}
{% endstylesheets %}

<link href="{{ asset('bundles/atcopcore/css/home.' ~ app.request.locale ~ '.css') }}" rel="stylsheet" />

Now assetic will create 4 files, home.de.css, home.en.css, home.it.css and home.es.css. And in your template your decide which css should be loaded.

like image 76
Markus Avatar answered Sep 18 '22 01:09

Markus