我正在使用renderWith()将数据对象数组渲染到模板但是有一些困难。除了基本的内容WYSIWYG编辑器中的内容之外,我现在所拥有的内容不会在指定的模板中呈现任何内容。
作为旁注,我使用短信模块作为参考,因此代码的设置方式与原因相同。
这是我在名为Drawer的数据对象中设置的函数,用于在DrawerContents
模板中显示FaqLisitng
的列表:
public function parse_shortcode($attributes, $content, $parser, $shortcode)
{
$drawers = "";
if (isset($attributes['Drawers'])) {
$Idx = explode(',', $attributes['Drawers']);
$drawerIdx = [];
foreach ($Idx as $did) {
$d = Drawer::get()
->filter('ID', $did)
->first();
if ($d->ID) {
array_push($drawerIdx, $d->ID);
}
}
$drawers = implode(',', $drawerIdx);
}
$drawerContents = DrawerContent::get()->filter(
array("DrawerID" => $drawers));
$drawerContentArray = array();
foreach($drawerContents as $dca){
$drawerContentArray = new ArrayData(array(
'FaqQuestion' => $dca->FaqQuestion,
'FaqAnswer' => $dca->FaqAnswer
));
}
return $drawerContentArray->renderWith('FaqListing');
}
这是FaqListing
模板内容:
<% with $getFaqListing %>
<strong>$FaqQuestion</strong><br/>
$FaqAnswer
<% end_with %>
我注意到如果我删除了with
块,我确实得到了parse_shorcode
函数中调用的每个DrawerContent数据对象的最后一个条目,只有第一个条目 - 而不是整个条目每个DrawerContent数据对象的列表。如果我使用循环而不是使用。
我不确定自己做错了什么,但我觉得我已经接近正确设置。
作为参考,如果它可以帮助提供解决方案,这里是Drawer
数据对象的完整代码:
<?php
class Drawer extends DataObject {
private static $db = array(
'FaqCategoryName' => 'varchar(250)',
);
private static $summary_fields = array(
'FaqCategoryName' => 'FAQ Category Name',
);
private static $has_many = array(
'DrawerContents' => 'DrawerContent',
);
/**
* Parse the shortcode and render as a string, probably with a template
Add a comment to this line
* @param array $attributes the list of attributes of the shortcode
* @param string $content the shortcode content
* @param ShortcodeParser $parser the ShortcodeParser instance
* @param string $shortcode the raw shortcode being parsed
* @return String
**/
public function parse_shortcode($attributes, $content, $parser, $shortcode) {
$drawers = "";
if (isset($attributes['Drawers'])) {
$Idx = explode(',', $attributes['Drawers']);
$drawerIdx = [];
foreach ($Idx as $did) {
$d = Drawer::get()
->filter('ID', $did)
->first();
if ($d->ID) {
array_push($drawerIdx, $d->ID);
}
}
$drawers = implode(',', $drawerIdx);
}
$drawerContents = DrawerContent::get()->filter(
array("DrawerID" => $drawers));
$drawerContentArray = array();
foreach($drawerContents as $dca){
$drawerContentArray = new ArrayData(array(
'FaqQuestion' => $dca->FaqQuestion,
'FaqAnswer' => $dca->FaqAnswer
));
}
return $drawerContentArray->renderWith('FaqListing');
}
public function getShortcodableRecords() {
return Drawer::get()
->map('ID')
->toArray();
}
/**
* Returns a list of fields for editing the shortcode's attributes
* in the insert shortcode popup window
*
* @return Fieldlist
**/
public function getShortcodeFields()
{
$all = Drawer::get()->map('ID');
$DrawersMultiList = ListboxField::create("Drawers", "Drawers")
->setMultiple(true)
->setSource($all);
return FieldList::create([
$DrawersMultiList,
]);
}
public function canView($member = null){
return true;
}
public function canEdit($member = null) {
return true;
}
public function canCreate($member = null) {
return true;
}
public function getTitle(){
$title = $this->FaqCategoryName;
return $title;
}
}
class DrawerAdmin extends ModelAdmin {
private static $managed_models = array('Drawer');
private static $url_segment = 'Drawers';
private static $menu_title = 'Drawers';
}
DrawerContent
数据对象的代码:
<?php
class DrawerContent extends DataObject {
private static $db = array(
'FaqQuestion' => 'varchar',
'FaqAnswer' => 'HTMLText',
);
private static $has_one = array(
'Drawer' => 'Drawer',
);
private static $summary_fields = array(
'FaqQuestion' => 'FAQ Question',
);
}
$ getFaqListing函数,它在Page.php
public function getFaqListing() {
return DrawerContent::get();
}