print Tutorial 2: Pulling additional view information

Model and view changes

The model

There are 2 ways of modifying the model to fetch the data that we need. The first is to leverage the existing listOfObjects method to support "paging" (as of 0.1-beta4 objects built using the default template will already include offset/limit support) and set this to return newest first; or: create a dedicated method to fetch just the top X etc.

How this is done and which to use is left as an exercise to the reader, this tutorial will use the listOfObjects method as built by the generator with a single modification to add ORDER BY createDate to the query.

With that done, the model changes are complete.

The view

Finally we come to the view. The changes to be made here have already been dictated by the controller in the controller changes. Remeber the method call through to the view we added? We now need to create this.

Open the guestbookView and add the following method:

// guestbookView.class.php
/**
 * Displays the top 5 most recent messages
 *
 * @return string
 */
function getStandaloneTop5View() {
	if ( !$this->isCached($this->getTpl('top5', 'guestbook')) ) {
		
	}
	return $this->compile($this->getTpl('top5', 'guestbook'));
}

This will compile and return our top 5 most recent entries from the guestbook. A template will need creating within the views/guestbook called top5.html.tpl.

Note how the site path is given to locate the template. This is very important, if it is not included, then the current request path is used which will not necessarily be to the guestbook.

Finally we add our model and a list of the top 5 entries:

$this->getEngine()->assign('oModel', utilityOutputWrapper::wrap($this->getController()->getModel()));
$this->getEngine()->assign('entries', utilityOutputWrapper::wrap(guestbook::listOfObjects(0, 5)));

Finally we need to create the template and arrange our layout. This could be a table, list or something more complicated. Again, this is left up to the reader to implement. For this tutorial (and for the sake of speed) this top5.html.tpl will simply feature:

{* top5.html.tpl*}
{$entries|printr}

Once done, the final step is to modify the home page to pull the data.