本文介绍了将变量传递给 Symfony2 中的 Assetic 资产 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将变量传递给模板中的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 READMEnoreferrer">此处.我希望这会很快改变.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:58
查看更多