使用记录值作为Twig函数参数

时间:2018-10-20 13:58:59

标签: php twig bolt-cms

我正在写一个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函数的参数?

0 个答案:

没有答案