我正在使用Silvertripe 3.0,目前有一个简单的页面设置,有几个子页面。子页面包含包含图像的数据对象,我试图让子页面的图像和URL显示在父页面上。我的模板中有以下代码:
<% loop $Children %>
<a href="$Link">$Title</a>
<% end_loop %>
<div id="whiskyMainGallery">
<% control WhiskyGallery %>
<div class="item">
<a href="$WhiskyPageID">$Slide.SetRatioSize(220,178)</a>
</div>
<% end_control %>
</div>
这两个控件都按预期工作,但我尝试将它们组合在一起,这样我就可以从WhiskyGallery控件获取数据对象图像以及URL,这样我就可以在模板中调用类似下面的内容:
<% control JoinedStuff %>
<a href="$ChildPageUrl">$DataObjectImage</a>
<% end_control%>
非常感谢任何帮助!
这是我的whiskyPage类:
class WhiskyPage extends Page {
public static $db = array(
'ABV' => 'Text',
'Colour' => 'Text',
'Aroma' => 'HTMLText',
'Taste' => 'HTMLText',
'Finish' => 'HTMLText'
);
public static $has_many = array(
'TastingAwards' => 'TastingAward',
'WhiskyImages' => 'WhiskyImage'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$gridFieldConfig = GridFieldConfig_RecordEditor::create();
$gridFieldConfig->addComponent(new GridFieldBulkEditingTools());
$gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$gridfield = new GridField("TastingAwards", "Tasting Awards", $this->TastingAwards()->sort("SortOrder"), $gridFieldConfig);
$gridFieldConfig2 = GridFieldConfig_RecordEditor::create();
$gridFieldConfig2->addComponent(new GridFieldBulkEditingTools());
$gridFieldConfig2->addComponent(new GridFieldBulkImageUpload());
$gridFieldConfig2->addComponent(new GridFieldSortableRows('SortOrder'));
$gridfield2 = new GridField("WhiskyImages", "Whisky Images", $this->WhiskyImages()->sort("SortOrder"), $gridFieldConfig2);
$fields->addFieldToTab('Root.Tasting Notes', new TextField('ABV', 'ABV'));
$fields->addFieldToTab('Root.Tasting Notes', new TextField('Colour', 'Colour'));
$fields->addFieldToTab('Root.Tasting Notes', new HTMLEditorField('Aroma', 'Aroma'));
$fields->addFieldToTab('Root.Tasting Notes', new HTMLEditorField('Taste', 'Taste'));
$fields->addFieldToTab('Root.Tasting Notes', new HTMLEditorField('Finish', 'Finish'));
$fields->addFieldToTab('Root.Tasting Awards', $gridfield);
$fields->addFieldToTab('Root.WhiskyImages', $gridfield2);
return $fields;
}
}
My WhiskyImage DataObject
class WhiskyImage extends DataObject {
public static $db = array(
'SortOrder' => 'Int',
'Title' => 'Varchar',
'Featured' => 'Boolean'
);
// One-to-one relationship with whisky page
public static $has_one = array(
'Slide' => 'Image',
'WhiskyPage' => 'WhiskyPage'
);
// tidy up the CMS by not showing these fields
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeFieldFromTab("Root.Main","WhiskyPageID");
$fields->removeFieldFromTab("Root.Main","SortOrder");
$fields->addFieldToTab('Root.Main', new CheckboxField('Featured', 'Featured'));
return $fields;
}
// Tell the datagrid what fields to show in the table
public static $summary_fields = array(
'ID' => 'ID',
'Title' => 'Title',
'Thumbnail' => 'Thumbnail'
);
// this function creates the thumbnail for the summary fields to use
public function getThumbnail() {
if ($Image = $this->Slide()->ID) {
return $this->Slide()->SetWidth(80);
} else {
return '(Please upload an image)';
}
}
}
WhiskyLandingPage有它自己的类,但没有什么有趣的,除了返回图像的自定义函数
public function GetWhiskyGallery() {
return WhiskyImage::get()->filter(array('Featured' => 1))->sort("SortOrder");
}
希望更有意义!
答案 0 :(得分:2)
首先,您应该在模板中使用loop
而不是control
第二,加入数据对象和页面似乎是一个相当糟糕的主意,你可能会遇到问题。 为什么不循环页面然后循环数据对象?
像这样:<% loop $Children %>
<a href="$Link">$Title</a>
<ul>
<% loop $WhiskyImages %>
<a href="$Top.Link" title="link to the whisky page">Image: $Title</a>
<a href="$WhiskyPage.Link">this link will also link to the wisky page</a>
$Slide.SetRatioSize(220,178)
<% end_loop %>
</ul>
<% end_loop %>
//编辑:
在您只是循环所有WhiskyImage
个对象列表的示例中,您可以这样做:
<div id="whiskyMainGallery">
<% loop WhiskyGallery %>
<div class="item">
<a href="$WhiskyPage.Link">$Slide.SetRatioSize(220,178)</a>
</div>
<% end_loop %>
</div>