PHP – TinyMVC Template Wrapper Plugin

TinyMVC provides a great structure for developing new projects, it’s small base allows the developer to write new plug-ins and extend on the existing framework. Below you’ll find a template wrapper I have made to manage the views and assigning of data. The class allows you to set a default view to be used in order to keep consistency throughout; so that you can minimize view code in the controllers.

The plug-in will also allow you to manage the errors accordingly, this will remove TinyMVC’s error blocks that occur due to missing or invalid template files and let you handle them yourself.

tinymvc_library_template.php

<?php

class TinyMVC_Library_Template {
	private $tmvc = null;
	private $theme = 'index_view';
	private $assigned = 0;
	private $errors = Array();

  function __construct() {
	if (!$this->tmvc) {
		$this->tmvc = tmvc::instance();
	}
  }

  function display($data) {
	if (isset($data) && isset($this->tmvc)) {
		try {
			$this->tmvc->controller->view->assign('data', $data);
			$this->tmvc->controller->view->display($this->theme);
		} catch(Exception $e) {
			$this->error($e->getMessage());
		}
	}
  }

  public function fetch($view) {
	if (isset($this->tmvc, $view)) {
		try {
			$this->tmvc->controller->view->fetch($view);
		} catch (Exception $e) {
			$this->error($e->getMessage());
		}
	}
  }

  public function assign($var, $val) {
	if (isset($var, $val, $this->tmvc)) {
		try {
			$this->tmvc->controller->view->assign($var, $val);
			$this->assigned++;
		} catch (Exception $e) {
			$this->error($e->getMessage());
		}
	}
  }

  private function error($msg) {
	if (isset($msg)) {
		$this->errors[] = $msg;
	}
  }

  public function getErrors() {
	return $this->errors;
  }
}

?>

Place the above code in a new PHP file called tinymvc_library_template.php and place inside the plugins directory, under your application folder.

You’ll notice the class var $theme is set to ‘index_view’ this should be your main HTML layout view for the site; the display function will then output all data inside of this view.

Using the plugin

You’ll need to load the plugin library manually, or you can auto-load it if you have it set up. Below is a snippet inside a controller to load the plugin library and use it, you don’t have to use it this way, it’s just an example.

<?php

class Default_Controller extends TinyMVC_Controller {
  function index() {
	/* Load the Plugin Library */
	$this->load->library('template');

	/* Fetch a view example */
	$data = $this->template->fetch('someview');

	if ($data) {
		/* Lets output the data we got from the view above */
		$this->template->display($data);
	} else {
		/* Output any errors that were encountered */
		print_r($this->template->getErrors());
	}
  }
}
?>

As you can see the above just prints an array of errors that the plugin encountered, you can manage these manually – Log them or just ignore them, it’s up to you. This can be extended on greatly and probably will be updated as and when it does.

About Steve

Web Developer, IT enthusiast & PC Gamer.
This entry was posted in PHP, PHP frameworks and tagged , , . Bookmark the permalink.

One Response to PHP – TinyMVC Template Wrapper Plugin

  1. Alex says:

    Great little plugin – Nice job

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>