我正在使用 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%>
任何帮助将不胜感激!
这是我的威士忌页面类:
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;
}
}
我的 WhiskyImage 数据对象
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");
}
希望这更有意义!