【问题标题】:How to loop over templates in silverstripe如何在 silverstripe 中循环模板
【发布时间】:2014-11-17 21:29:00
【问题描述】:

你好

我在 silverstripe 中创建一个单页网站,并创建了一些模板来代表我的不同网站部分。

我首先尝试使用'include'进行循环:

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

但从我在论坛上发现的情况来看,这不是这样做的方法,并且我收到了一个错误,缺少循环标记...所以我尝试创建一个函数

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

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

大惊喜...它是一样的,因为我读到 include 和 renderWith 都做同样的工作。 好吧,我真的不知道一个好的解决方案,如果我的模板名称像 X,我想实现比包含一些模板更复杂的东西,例如。

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

如果你知道一个好的解决方案,请写在这里!那会让我很开心:D。

谢谢, 托马斯

【问题讨论】:

    标签: php content-management-system silverstripe


    【解决方案1】:

    听起来你正在尝试做的类似于一个名为 Content Blocks 的模块。

    在该模块中,它们具有相同的情况,即循环遍历块并为该块插入正确的模板。

    模块的Page.ss:

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

    $ActiveBlocks 指的是function on a DataExtension by the same name

    这是您已经熟悉的所有内容。您可能不太了解的部分是模板中的$Me 值。 It can be used to refer to the current object context the template is rendered with,在你的情况下是Slug

    这只是魔术的一部分,另一部分是BlockDataObject 上的一个函数,称为forTemplate。使用$Me 计算如何渲染DataObject 时调用此函数。

    在这里,您可以像这样执行经典的renderWith 调用:

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

    有了这些知识,您可以通过使Slug DataObject 看起来像这样来实现您所追求的目标:

    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'));
        }
    }
    

    您的模板看起来像这样:

    <% if $Slugs %>
        <% loop $Slugs %>
            $Me
        <% end_loop %>
    <% end_if %>
    

    【讨论】:

      【解决方案2】:

      我在onepage module 中做了类似的事情

      试试这样的:

          /**
          * renders the current page using the ClassName_onepage template,
          * e.g. Page_onepage
          *
          * @return HTMLText
          */
          public function getOnePageContent(){
          $templateName = SSViewer::get_templates_by_class($this->owner->Classname, '_onepage', 'SiteTree')
          ?: 'Page_onepage';
          return $this->renderWith($templateName);
          }
      

      在你的模板中:

          <% if SlugItems %>
              <% loop SlugItems %>
                  $OnePageContent
              <% end_loop %>
          <% end_if %>
      

      HTH,

      wmk

      【讨论】:

      • 感谢您的解决方案。这是一个很好的,但我认为第一个更干净
      【解决方案3】:

      您可以定义一个模板文件,例如 Slug.ss,在其中放置要循环的 HTML,并将其保存到主题/您的主题/模板/包含中。

      然后你可以用这段代码循环它:

      <% if SlugItems %>
          <% loop SlugItems %>
              <% include Slug %>
          <% end_loop %>
      <% end_if %>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-07
        • 2013-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-04
        • 2013-10-26
        • 2013-11-22
        相关资源
        最近更新 更多