如何获取子页面并在其他页面类型上循环显示其数据?

时间:2015-10-26 05:18:02

标签: silverstripe

我有一个 self.currentVC.willMoveToParentViewController(nil) self.addChildViewController(newController) self.view.addSubview(newController.view) newController.view.alpha = 0 newController.view.layoutIfNeeded() UIView.animateWithDuration(0.5, animations: { newController.view.alpha = 1 self.currentVC.view.alpha = 0 }, completion: { finished in self.currentVC.view.removeFromSuperview() self.currentVC.removeFromParentViewController() newController.didMoveToParentViewController(self) }) 页面类型,其子页面类型为TestimonalHolder,每个类型都有Testimonials $ db字段来存储证词。

问题是如何访问我Message上的$Message字段,以便我可以循环浏览它们并将它们放入滑块等。

Testimonials.php

HomePage.ss

我知道我可以在我的class Testimonials extends Page { private static $db = array( 'Message' => 'Text' ); public function getCMSFields() { $fields = parent::getCMSFields(); $fields->addFieldsToTab('Root.Testimonials', array( TextareaField::create('Message') )); return $fields; } } class Testimonials_Controller extends Page_Controller { } 页面上使用此代码来遍历它们:

TestimonialHolder.ss

2 个答案:

答案 0 :(得分:7)

在您的HomePage.php中

public function getTestimonials($limit = 5) {
    return Testimonials::get()->limit($limit);
}

然后在您的模板中,像$Testimonials一样使用$Children

答案 1 :(得分:0)

在你的模板中:

history_url

使用此XX帮助程序,您可以直接在模板中访问一个简单的数据列表(即使<% loop $Testimonials('BlogPost').Limit(5) %> $Title <% end_loop %> $ListFilter()),但这有点违反纯MVC

小心处理!

另请参阅:http://www.silverstrip.es/blog/retrieving-some-dataobjects-in-template-without-php-getter/(免责声明:我的博客)