我正在尝试编写一个显示Holder Page上子页面图像的函数。
由于SilverStripe在模板上缺少某些功能,我认为最好在控制器中处理它。
我需要一些条件语句,只能在php中完成。
Controller.php这样
public function LatestWork() {
$works = WorkPage::get();
$i = 1;
$html = "";
foreach ($works as $work) {
//Build the IMage Object so we can add it to the Work Object
$ImageObj = File::get()->byID($work->FeaturedImageID);
if ($this->is_odd($i)) {
$html .= "<div class='row'>";
$span = "span8";
} else {
$span = "span4";
}
$html .= "<div class = '$span'>" . $ImageObj->croppedImage(200,100) . "</div>";
if ($this->is_even($i) || $i == $works->Count()) {
$html .= "</div>";
}
$i++;
}
return $html;
}
当在视图中处理div和span时,但是图像不是。代码中有更多条件,但这只是基本版本。 它显示“Image_Cached”。
如何让它显示图像?
答案 0 :(得分:1)
控制器:
public function LatestWork() {
$rows=new ArrayList();
foreach(WorkPage::get() as $workPage){
if (!isset($bucket)){
$bucket = new ArrayList();
$bucket->push($workPage);
$rows->push($bucket);
} else {
$bucket->push($workPage);
unset($bucket);
}
}
return $rows;
}
模板:
<% loop LatestWork %>
<div class="row">
<% if Odd %>
<div class="span-8">
<% with $Me.First %>$FeaturedImage.CroppedImage(200,100)<% end_with %>
</div>
<div class="span-4">
<% with $Me.Last %>$FeaturedImage.CroppedImage(100,50)<% end_with %>
</div>
<% else %>
<div class="span-4">
<% with $Me.First %>$FeaturedImage.CroppedImage(100,50)<% end_with %>
</div>
<div class="span-8">
<% with $Me.Last %>$FeaturedImage.CroppedImage(200,100)<% end_with %>
</div>
<% end_if %>
</div>
<% end_loop %>
将是SS的方式,因此您的显示逻辑不会使控制器混乱