问题描述
是否可以通过模板将变量传递给Assetic方法
Is there a way to pass a variable to the Assetic method in templates
{% stylesheets
'@SomeExampleBundle/Resources/views/ SOMEVAR /css/*'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
所以我要做的是从控制器传递SOMEVAR
.
So what I want to do is pass SOMEVAR
from the controller.
推荐答案
就目前而言,我认为根本不可能.其背后的原因是Assetic
会先运行以转储资产,因此它不会运行Twig模板来计算变量.如果在PHP模板中执行此操作,则可能是相同的.
For now, I don't think it is possible at all. The reason behind this is that Assetic
is run upfront to dump the assets, so it does not run the Twig template to compute the variable. This is probably the same if you do it in a PHP template.
这意味着将不会计算和扩展运行时变量.因此,如果使用变量,则无法生成资产.将来可能会有所变化,但是每次用户请求资产时都会产生生产开销,因为Assetic
需要生成资产.
This means that runtime variables will not be computed and expanded. Thus, this make it impossible to generate the assets if a variable is used. This may change in the future, but this would incur an overhead in production each time the assets are requested by the user because Assetic
would need to generate the assets.
我知道可以直接使用Assetic
中的代码(而不是使用AsseticBundle
)以编程方式定义和生成资产.您需要进行实验,阅读源代码,并进行反复试验才能解决此问题.
I know it is possible to programmatically defines and generates the asset by using the code found in Assetic
directly (not by using the AsseticBundle
). You will need to experiment, read the source code, and do trials and errors to work out off this problem.
目前在Assetic
上几乎没有文档.我唯一可以给出的链接是在Assetic的github页上找到的README
此处.我希望这会很快改变.
There is little to no documentation on Assetic
at the moment. The only link I can give is the README
found on the github page of Assetic here. I hope this will change soon.
希望这会有所帮助.
这篇关于将变量传递到Symfony2中的Assetic资产URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!