2. Responses

The main goal of an application is to ultimately return a kind of response. Inside front/page, you will see page response classes ending in php and template files ending in phtml. Where we differ from our Symfony and Zend Framework neighbors is how we think about processing actions. First off, there is no automated correlation from page class to template. We need to define what template belongs to what page class in that same page class. Second we don't mash up every action into one file. We separate them into page classes. Figure 1 shows us what's inside of front/page/index.php.

Figure 1. front/page/index.php
class Front_Page_Index extends Front_Page {
	/* Constants
	-------------------------------*/
	/* Public Properties
	-------------------------------*/
	/* Protected Properties
	-------------------------------*/
	protected $_title = 'Eden';
	protected $_class = 'home';
	protected $_template = '/index.phtml';
	
	/* Private Properties
	-------------------------------*/
	/* Magic
	-------------------------------*/
	/* Public Methods
	-------------------------------*/
	public function render() {
		return $this->_page();
	}
	
	/* Protected Methods
	-------------------------------*/
	/* Private Methods
	-------------------------------*/
}

The only required method when extending Front_Page is render(). The purpose of render() is to gather all the variables needed for a template we'll eventually parse into a string and return it. Commonly we can summarize a page load into a set algorythm defined in _page(). The definition of that method is shown in Figure 2.

Figure 2. front/page.php - _page() Method Definition
protected function _page() {
	$this->_head['page'] = $this->_class;
	
	$page = front()->path('page');
	$head = front()->trigger('head')->template($page.'/_head.phtml', $this->_head);
	$body = front()->trigger('body')->template($page.$this->_template, $this->_body);
	$foot = front()->trigger('foot')->template($page.'/_foot.phtml', $this->_foot);
	
	//page
	return front()->template($page.'/_page.phtml', array(
		'meta' 			=> $this->_meta,
		'title'			=> $this->_title,
		'class'			=> $this->_class,
		'head'			=> $head,
		'body'			=> $body,
		'foot'			=> $foot));
}

Here we expose some ideas you can do inside of your render() method. The first major thing we do is render a template called _head.phtml, This kind of template is called a partial. A partial versus a block, does not have any special configuration. If trigger() does not look familiar to you, please read Events.

Next we render the body where the template file is specific to the page class. For example, front/page/index.php sets this by protected $_template = '/index.phtml' under the protected properties category.

After we render the body, we render a template called _foot.phtml. All of these template parts will be joined when we render _page.phtml, which is the final output. By setting it up this way, a page class, only needs to worry about rendering the body of the page, because both the head and the foot will be reused. A full list properties you can set in your page are found in Figure 3.

Figure 3. Protected Properties
protected $_meta	= array();		//a key value array for meta tags
protected $_head 	= array();		//variables that will be binded to the _head template
protected $_body 	= array();		//variables that will be binded to the body template
protected $_foot 	= array();		//variables that will be binded to the _foot template

protected $_title 		= NULL;		//sets the title tag
protected $_class 		= NULL;		//adds a unique class name to the html tag
protected $_template 	= NULL;		//the path to the body template

Going back to front/page/index.php, we can bind variables to the front/page/index.phtml simply as in Figure 4.

Figure 4. Add a Binded Variable on front/page/index.php
public function render() {
	$this->_body['test'] = 'testing';
	return $this->_page();
}

In the figure above, we simply binded a variable called test, then loaded the default page process. Given the test example above, it should be very clear that in our page class, we are only suppose to working on gathering data to be used on a template. We can thus, deem it bad programming practice if a template gathers data for output. A template should simply make output with the binded variables passed and nothing more. Figure 4a, shows how we can access the test binded variable.

Figure 4a. Using that binded variable on front/page/index.phtml
<?php echo $test; ?>

Next, Figure 5 and Figure 5a shows how we would retreive data from a database, assign that to a binded value and use that binded value for output.

Figure 5. Dynamic Binded Variable on front/page/index.php
public function render() {
	$this->_body['post'] = front()->getDatabase()->getRow('post', 'post_id', 1);
	return $this->_page();
}
Figure 5a. Using dynamic binded variable on front/page/index.phtml
<h3><?php echo $post['post_title']; ?></h3>
<p><?php echo $post['post_detail']; ?></p>

© 2012 Openovate Labs. All rights reserved.