php - 使用记录值作为 Twig 函数参数
问题描述
我正在编写一个 Bolt 扩展来解析 XML 文件并返回 XML 的反序列化对象树。解析和反序列化工作就像一个魅力,但我无法从页面记录中获取文件名作为 Twig 函数的参数。
扩展代码(简化)是:
class CompetitionExtension extends SimpleExtension
{
protected function registerTwigFunctions()
{
return [
'competition' => 'competitionFunction',
];
}
/**
* Load and parse the competition XML.
*
* @param string $filename
* @return Competition
*/
public function competitionFunction(string $filename) : Competition
{
$competition = null;
$loader = new FileLoader($filename);
if ($loader->openFile()) {
$competition = $loader->parse();
}
return $competition;
}
}
(contenttype
扩展的homepage
)添加是:
competitionxml:
type: file
upload: competitions
group: content
文件的上传也没有问题,但是下面的 Twig 模板代码给出了错误:
{% set xmlfile = homepage.competitionxml %}
{% set comp = competition(xmlfile) %}
使用dump
我看到它xmlfile
具有正确的值。但我收到以下错误:
An exception has been thrown during the rendering of a template ("Notice: Undefined variable: filename") in "index.twig" at line 27.
那么如何使用附件的文件名作为 Twig 函数的参数呢?
解决方案
推荐阅读
- python - 我应该创建一个我经常使用的函数的python模块吗
- javascript - 当窗口在 IE 11 和其他较低版本中实际打开时,弹出/子窗口属性“关闭”始终为 true
- github - 如何将 Github 与 AWS Glue 中的数据目录集成
- java - 在webflux中,WebFilter和HandlerFilterFunction有什么区别
- c# - 写清单
使用 System.Text.Json 到没有缩进的 Json 数组 - django - 在开发环境中本地提供 django 媒体文件
- r - nls r 中分段函数的起始值
- javascript - 使用路径字符串从另一个文件中查找文件的相对路径
- javascript - jQuery 嵌套循环
- delphi - 是否有将 DBGRID 大量数据导出为 CSV 和 XLS 的功能?