doufan6886 2014-11-17 21:29
浏览 26
已采纳

如何在silverstripe中循环模板

Hello,

I'm doing a one-page site in silverstripe and i have created some templates that will represent my different site parts.

I first tried to loop by using 'include' so :

<% if SlugItems %>
    <% loop SlugItems %>
        <% include $slug %>
    <% end_loop %>
<% end_if %>

But from what i found on forums, it's not the way to do it and i got an error with loop tag missing...So i tried to create a function

public function IncludeTemplate($template) {
    return $this->renderWith($template);
}

And

<% if SlugItems %>
    <% loop SlugItems %>
        <% IncludeTemplate($Slug) %>
    <% end_loop %>
<% end_if %>

Big surprise...It's the same, cause i read that both include and renderWith do the same job. Well, i don't really know a good solution and i'd like to implement something more elaborate than including some template if my template name is like X, eg.

<% if $ClassName = 'SomeClass' %>
    <% include SomeClass %> 
<% else_if $ClassName = 'SomeOtherClass' %>
    <% include SomeOtherClass %>
<% else %>
    <% include DefaultClass %>
<% end_if %>

If you know a good solution, please write it here! That would make my day :D.

Thanks, Thomas

  • 写回答

3条回答 默认 最新

  • douyalin0847 2014-11-18 10:18
    关注

    What it sounds like you are trying to do is similar to a module called Content Blocks.

    In that module, they have an identical situation where they loop over blocks and insert the right template for that block.

    The module's Page.ss:

    <% loop $ActiveBlocks %>$Me<% end_loop %>
    

    $ActiveBlocks refers to a function on a DataExtension by the same name.

    This is all stuff that you are already familiar with. The part that you might not know so much about is the $Me value in the template. It can be used to refer to the current object context the template is rendered with, in your case that would be a Slug.

    That is only part of the magic, the other part is a function on the Block DataObject the called forTemplate. This is called when using $Me to work out how to render the DataObject.

    In here, you can just perform a classic renderWith call like this:

    return $this->renderWith(array($this->Template, 'Slug'));
    

    With this knowledge, you could achieve what you are after by having a Slug DataObject looking something like this:

    class Slug extends DataObject
    {
        static $db = array();
    
        function getTemplate()
        {
            $template = 'yourTemplate';
    
            //Do your template logic checks in here to work out what you want to display
    
            return $template;
        }
    
        function forTemplate()
        {
            return $this->renderWith(array($this->Template, 'Slug'));
        }
    }
    

    With your template looking something like this:

    <% if $Slugs %>
        <% loop $Slugs %>
            $Me
        <% end_loop %>
    <% end_if %>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址