我正在写一个(Symfony2)SmartyBundle扩展来支持Assetic .
为了支持stylesheets feature我已经注册了一个名为 stylesheets
的块插件:
{stylesheets
assets='@SmartyBundle/Resources/public/css/*'
debug=true}
<a href="{$asset_url}">{$asset_url}</a>
{/stylesheets}
正确调用此插件,并且在创建Symfony / assetic缓存时,一切都按预期工作 .
问题出现当Symfony缓存为空并且Assetic加载每个模板文件资源并要求模板引擎检索带有stylesheets标记中找到的标记的PHP数组时 . 被调用以检索数组的类是 SmartyFormulaLoader
.
<?php
class SmartyFormulaLoader implements \Assetic\Factory\Loader\FormulaLoaderInterface
{
public function load(ResourceInterface $resource)
{
// raw template content
$content = $resource->getContent();
// a FileLoaderImportCircularReferenceException is throw here
$smartyParsed = $this->smarty->fetch('string: '.$content);
// build an array with tokens extracted from the block function
$formulae = $this->extractStylesheetsTokens($smartyParsed);
return $formulae;
}
在 load()
方法中调用 $smarty->fetch()
时,将引发异常: Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException: Circular reference detected in "." ("." > ".")
.
这是由Smarty模板被解析/编译并再次调用 stylesheets
插件引起的 .
所以我问Smarty是否提供了一个模板解析器来提取块函数标记(不调用样式表插件),所以我可以提供Assetic . 或者我可能缺少任何其他解决方案来解决这个问题 .
谢谢 .
1 回答
在与Smarty dev @rodneyrehm聊了一会后,我们得出结论:
应该解析原始模板源而不是编译模板并解析它
应该编写我自己的解析器(而不是使用一些Smarty内部帮助器)
因此,对于这个特殊情况,我们提出了这个实现:
完整实施可在此处获取:https://github.com/noiselabs/SmartyBundle/blob/master/Assetic/SmartyFormulaLoader.php