Symfony get Asset Name in Assetic 输出

我用这样的代码生成我的文件:

{% block head_stylesheets %}
{% stylesheets
filter='?uglifycss'
'@MyBundle/Resources/public/less/myfile.less'
%}
<link href="{{ asset_url }}" type="text/css" rel="stylesheet" media="screen"/>
{% endstylesheets %}
{% endblock head_stylesheets %}

它转储一个名为 c543k540_myfile_1.css的文件

这个文件由资产名后跟文件名 {assetName}_{filename}.css组成

如何在输出中保留资产名称来自定义输出?

{% block head_stylesheets %}
{% stylesheets
output="css/mynewfilename.{assetName}.css"     // <--- Here
filter='?uglifycss'
'@MyBundle/Resources/public/less/myfile.less'
%}
<link href="{{ asset_url }}" type="text/css" rel="stylesheet" media="screen"/>
{% endstylesheets %}
{% endblock head_stylesheets %}



更新以澄清

我知道如果我使用 name 选项,在 prod 中它将编译成 myouputname.css,但是我希望在 debug/dev 环境中使用下面的代码,比如 myfile_myouputname_1.css

{% block head_stylesheets %}
{% stylesheets
name="myouputname"     // <--- Here
filter='?uglifycss'
'@MyBundle/Resources/public/less/myfile.less'
%}
<link href="{{ asset_url }}" type="text/css" rel="stylesheet" media="screen"/>
{% endstylesheets %}
{% endblock head_stylesheets %}

谢谢你的回复。

2748 次浏览

Using Assetic to manage web assets in Symfony applications is no longer recommended. Instead, use Webpack Encore. https://symfony.com/doc/current/frontend/assetic/index.html#dumping-asset-files

Page-Specific JavaScript or CSS (Multiple Entries) is described here: https://symfony.com/doc/current/frontend/encore/simple-example.html#multiple-javascript-entries

// webpack.config.js
Encore
// ... general CSS file here...
.addStyleEntry('some_page', './assets/css/assetName.css')
;

Using addStyleEntry() is supported, but not recommended. A better option is to follow the pattern above: use addEntry() to point to a JavaScript file, then require the CSS needed from inside of that.