[英]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. 我使用Symfony2进行多本地化项目,并根据当前用户本地在不同文件夹上分离(CSS / Images)。
Folder Structure Ex: 文件夹结构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 | **现在我需要Assetic来呈现基于当前用户本地{ar |的正确home.CSS文件 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 : 注意:我想获得css compine的优势,如果我执行以下操作,则无法执行此操作:
<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 如何根据会话中的值嵌入带有Assetic的样式表
Please , Any suggestions ? 请问,有什么建议吗?
Assetic provides an functionality for you problem. Assetic为您提供功能问题。
Add this to your configuration 将其添加到您的配置中
assetic:
# more from assetic
variables:
locale: [de, en, it, es] # whatever
And in your twig file: 在你的twig文件中:
{% 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. 现在,assetic将创建4个文件,home.de.css,home.en.css,home.it.css和home.es.css。 And in your template your decide which css should be loaded.
在您的模板中,您决定应该加载哪个CSS。
You maybe should try something like this: 你可能应该尝试这样的事情:
{% if app.request.locale == "en" %}
{% stylesheets filter='cssrewrite'
'bundles/atcopcore/css/en/home.css'
'bundles/atcopcore/css/en/foo.css'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
{% elseif app.request.locale == "fr" %}
{% stylesheets filter='cssrewrite'
'bundles/atcopcore/css/fr/home.css'
'bundles/atcopcore/css/fr/foo.css'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
{% elseif app.request.locale == "ar" %}
{% stylesheets filter='cssrewrite'
'bundles/atcopcore/css/ar/home.css'
'bundles/atcopcore/css/ar/foo.css'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
{% endif %}
It looks a little bit verbose, but this way all your css files will be combined and good one will be served to the user. 它看起来有点冗长,但这样所有的css文件将被合并,并且将向用户提供好的文件。
This is a trick 这是一个技巧
Your Twig file 你的Twig文件
{% stylesheets
'@AcmeBundle/Resources/public/css/css.css' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
Your '@AcmeBundle/Resources/public/css/css.css' 你的'@ AcmeBundle / Resources / public / css / css.css'
/* acme_css */
@import url('/css.css');
Your routing.yml 你的routing.yml
acme_css:
path: /css.css
defaults: { _controller: AcmeBundle:NameOfYourConroller:css}
Your controller 你的控制器
public function cssAction()
{
$request = $this->get('request');
// all your css files here
$css = array(
"@import url('/css/".$request->getLocale().".css.css');",
);
return new Response(implode(';',$css), 200, array('Content-Type' => 'text/css'));
}
NB : To test dont forget to empty your cache 注意:测试不要忘记清空缓存
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.