Rendering the Most Recent Pages
To get started, create a new method in the Page model to fetch the most recently created pages. This method will need to fetch the pages in the order that they were created. It then needs to cycle through these pages, opening each one. Finally, it should return these pages as an array that you can pass to the view (Listing 6-17).
Listing6-17. ThegetRecentPages() Method in application/models/Page.php public function getRecentPages ($count = 10, $namespace = 'page') {
$select->order = 'date_created DESC';
//cycle through the results, opening each page
foreach ($results as $result) {
$pages[$result->id] = new CMS_Content_Item_Page($result->id);
return null;
The three most recent items will be featured items. These items will be rendered as blocks with thumbnails and the full description. The remaining recent items will simply be links.
To do this, you need to create a new instance of the page model and fetch the results of the getRecentPages() method. Once you have this array, shift the first three items off the beginning of the array, and pass these to the view as the featured items. Pass the remaining array to the view as the recent pages, as shown in Listing 6-18.
Listing 6-18. The Index Action in application/controllers/PageController.php public function indexAction() {
$pageModel = new Model_Page(); $recentPages = $pageModel->getRecentPages();
if(is_array($recentPages)) {
// the 3 most recent items are the featured items for($i = 1; $i <= 3; $i++) {
$featuredItems[] = array_shift($recentPages);
$this->view->featuredItems = $featuredItems;
$this->view->recentPages = $recentPages; } else {
Now you need to update the index.phtml view script to render these. The first three items, or featured items, will be rendered as blocks. These blocks will consist of the headline, a thumbnail (which you will just resize with CSS), and the page description. The remaining items will be rendered as a list of links, as shown in Listing 6-19.
Listing 6-19. The Page Index Action View Script in application/views/scripts/page/index.phtml
<h2>Featured Content</h2> <?php if($this->featuredItems) {
foreach ($this->featuredItems as $page) {
<div class='featuredItem'>
<h3><a href='/page/open/id/<?php echo $page->id; ?>'>
<?php echo $page->headline?></a></h3> <img src='<?php echo $page->image;?>' alt='<?php echo $page->headline?>' /> <p><?php echo $page->description; ?></p> <br style='clear:left;' /> </div>
if($this->recentPages) { ?> <div class='recentPages'> <h3>More Pages</h3> <ul>
<?php foreach ($this->recentPages as $page) {?> <li><a href='/page/open/id/<?php echo $page->id; ?>'>
<?php echo $page->headline?></a></li> <?php } ?> </ul> </div> <?php } ?>
Now when you point your browser to http://localhost/page, you should see the list with no particular style. Take a moment now to style the featuredItem blocks by updating /public/skins/blues/css/layout.css, as shown in Listing 6-20.
Listing 6-20. Adding the featuredItem Styles to the Blues Skin in /public/skins/blues/css/layout.css
.featuredItem{
padding:5px 10px; margin:10px; width:420px; min-height:100px; -moz-border-radius: 5px; border:1px solid #749BCE;
.featuredItem h3{
margin-bottom:5px;
.featuredItem h3 a{ color:#000; text-decoration:none;
.featuredItem h3 a:hover{
text-decoration:underline;
.featuredItem img{ width:120px; height:auto; float:left; margin:0 10px 10px 0;
.featuredItem ul{ padding:5px 30px;
Now when you refresh your browser, the featured item blocks should be cleanly laid out with the headline, a thumbnail, and the description, as shown in Figure 6-2.
- Figure 6-2. The styled featured page list
Post a comment