0

我正在开发一个 Silverstripe 4 项目,我们需要从路径中包含一个 SS 模板文件。

这是一个简单的例子,给出了我想要实现的要点。

class ExampleController extends ContentController
{    
  public function IncludeTemplateFromFilePath() {
    var $FilePath = '/path/to/file';
    ???
    return $output
  }
}

模板语法:

<div>$IncludeTemplateFromFilePath</div>

我查看了SSViewer 文档并查看了 Silverstripe 源代码,但无法找出正确的语法来完成这项工作。

有很多例子:

return SSViewer::get_templates_by_class(static::class, $suffix, self::class);

但是从文件路径中获取模板的语法是什么?

4

1 回答 1

1

我相信你可以做到以下几点:

public function IncludeTemplateFromFilePath()
{
    return SSViewer::execute_string(
        file_get_contents('/path/to/Template.ss'),
        [
            'Content' => 'Value that will be in $Content when used in /path/to/Template.ss'
        ]
    );
}

参考:http ://api.silverstripe.org/4/SilverStripe/View/SSViewer.html#method_execute_string

于 2018-06-23T12:19:43.150 回答