Adding a Class to Parse Views to an HMVC Design Pattern Framework - Implementing a method for rendering view files
(Page 4 of 4 )
As you’ll recall from the previous section, the only thing that needs to be done to complete the earlier “View” class is defining a method that precisely parses the content embedded in its template file. In keeping with this requirement, below I listed the finished version of the class, this time including this parsing method. Here it is:
(View.php)
<?php
class View
{
private $_viewfile = 'default_view.php';
private $_properties = array();
// factory method (chainable)
public static function factory($viewfile = '')
{
return new self($viewfile);
}
// constructor
public function __construct($viewfile = '')
{
if ($viewfile !== '')
{
$viewfile = $viewfile . '.php';
if (!file_exists($viewfile))
{
throw new Exception('View file does not exist.');
}
$this->_viewfile = $viewfile;
}
}
// set undeclared model property
public function __set($property, $value)
{
if (!isset($this->_properties[$property]))
{
$this->_properties[$property] = $value;
}
}
// get undeclared model property
public function __get($property)
{
if (isset($this->_properties[$property]))
{
return $this->_properties[$property];
}
}
// populate view with data and return contents
public function display()
{
extract($this->_properties);
ob_start();
include($this->_viewfile);
return ob_get_clean();
}
}// End View class
Done. As seen above, the “View” class has a new method called “display()” (feel free to name it anything you want), which uses an output buffer for parsing the PHP code embedded in the corresponding template file. At the end of this process, the corresponding output is returned to client code, after flushing and closing the pertinent buffer.
With the addition of the view parser class, the framework is finally complete and ready to be tested. The best way to do this is by building a simple database-driven application that puts all of the classes created so far to work under the schema of the HMVC design pattern.
That next step will be taken in the next tutorial.
Final thoughts
That’s all for now. Over this sixth installment of the series I added to the HMVC-based framework a class aimed at parsing view files. After coding this last core component, we're finally free to take a deep breath and relax, as the development of the framework is over.
But, wait a minute! Isn’t this a series supposed to explain the use of the HMVC design pattern? Yes, it is. And now that the framework is finally set, it’s time to give it a try by building a basic application that implements the pattern in a concrete case.
The database schema, along with the controllers of this sample application, will be defined in the upcoming tutorial. Don’t miss the next part!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.