Advanced Views

article_Swimming_Pool_View_1.jpgSince PHPDevShell V3.1.4+ only;

PHPDevShell has a unique and simple way of splitting your MVC. However, we found that in some instances this is not enough to keep the view neat and clean. It was very possible that you ended up pushing javascript from within your controller. With a large and complicated project this will quickly become messy.

Another scenario is where you don't want to use Smarty or any kind of template engine. Although PHPDevShell does allow php view files, this too is not OOP and gets very messy quickly. This made us introduce a new way of handling views, views through classes. The idea is to move anything that is a mix of PHP, shared instances and javascript there. Those that do not enjoy being in the tpl either.

Like models and views join the controller, so does the views/someName.view.php file. The file location would be the same as your tpl, except this will be a class file.

Lets look at an example of where the view class is being looked for; If he had a controller, named readme.php;

Screenshot at 2012-03-08 15:21:52.png

Nice, now we know where a view class will be looked for, as it is clear that it is related to the controllers name. a view class is created exactly like a controller class, very simple.

<?php

class readmeView extends PHPDS_view
{
	public function execute()
	{
		echo '

Hello World!

'; } } return 'readmeView';

This will now execute once the controller runs, you still have access to all the shared PHPDevShell properties and methods, now you can go wild with views.

PHPDevShell used to add allot of jquery changes by default, this was not a good practice as it became heavy and many pages did not need many of the elements. To rectify this we split these jquery functions up and it can be easily called to make your page look the same as the rest of PHPDevShell. Here they are...

  • $template->styleForms();
  • $template->styleButtons();
  • $template->styleFloatHeaders();
  • $template->styleTables();
  • $template->stylePagination();

For instance, say we have some javascript we want in the head tag of the template, we can silently push this through. Also note that I am calling some other styling elements which jquery does for us, it is a shortcut to modules.

<?php

class readmeView extends PHPDS_view
{
	public function execute()
	{
		$template = $this->template;

		$template->styleFloatHeaders();
		$template->styleTables();
		$template->styleButtons();

		$this->template->addJsFileToHead('themes/cloud/js/quickfilter/jquery.quickfilter.js');
	}
}

return 'readmeView';

Using the class as a view with HTML in it...

The power comes with the ability that you can pass values, arrays and objects from the controller to the view class. We will now create a simple view page using only the PHPDS_view class, no tpl file will be needed for instance;

Pass values from the controller like this;

<?php

class readmeExample extends PHPDS_controller
{
	public function execute()
	{
		$this->set('foo', 'Foo Bar');
		$this->set('other', array("Hello", "World"));
	}
}

return 'readmeExample';

Now we can catch this from the view class again, and have a view that looks something like this as an end result;

<?php

class readmeView extends PHPDS_view
{
	public function execute()
	{
		$template = $this->template;

		$template->styleFloatHeaders();
		$template->styleTables();
		$template->styleButtons();

		$this->template->addJsFileToHead('themes/cloud/js/quickfilter/jquery.quickfilter.js');

		$var = $this->helloWorld();
		echo "

{$var[0]} {$var[1]}

"; echo "

{$this->get('foo')}

"; // We can also go mad and send js or css to the head to change the overall design of the master theme. // Get ajax search ajax menu. $livesearch = $this->navigation->buildURL(null, "callback="); // For live ajax search we need to throw some javascript to the template head. $JS = <<template->addJsToHead($JS); // This is not where it stops, lets pass on some css. $this->template->addCSSToHead('.class { font-weight: bold; }'); } public function helloWorld() { return $this->get('other'); } } return 'readmeView';

We hope this tutorial gives you a good idea to the power achievable through the view class mechanism.

PHPDevShell © 2010 - All rights reserved.