Silverstripe - 是否可以在include语句中使用变量?

Kat*_*ndl 3 variables templates include silverstripe

我正在使用silverstripe模板,我想循环遍历当前页面的子页面,并根据该子页面的类型动态输入"include"控件中的模板名称.

这是我到目前为止的代码:

    <div id="tertiary-content">                   
        <% if $Children %>
            <% loop $Children %>
                <% include $ClassName %>
            <% end_loop %>
        <% end_if %>
    </div>
Run Code Online (Sandbox Code Playgroud)

(我的模板中有ss文件/包含与$ ClassName变量相关的目录)

这是我得到的错误:

错误是:遇到未知的打开块"循环".也许你错过了结束标签或错误拼写了?

我在silverstripe论坛上发现了这篇文章,这让我觉得它应该有用:http: //www.silverstripe.org/archive/show/1023

实际上是否可以在include控件中包含变量?

Rya*_*n M 7

您可以在Page类中编写一个函数,该函数根据当前的类名加载ss模板.在您的Page.php文件中.

class Page extends SiteTree {

/**
 * Returns a template based on the current ClassName
 * @return {mixed} template to be rendered
 **/
public function getIncludeTemplate(){
    return $this->renderWith($this->ClassName);
}

}
Run Code Online (Sandbox Code Playgroud)

然后在你的模板中

<div id="tertiary-content">                   
    <% if $Children %>
        <% loop $Children %>
            $IncludeTemplate
        <% end_loop %>
    <% end_if %>
</div>
Run Code Online (Sandbox Code Playgroud)


ntd*_*ntd 5

您可以renderWith直接从模板调用,例如:

<div id="tertiary-content">                   
    <% if $Children %>
        <% loop $Children %>
            $renderWith($ClassName)
        <% end_loop %>
    <% end_if %>
</div>
Run Code Online (Sandbox Code Playgroud)