Views can be used to create both web pages and HTML mail, while maintaining a division between the logic and layout of your page. This crucial split makes updates easier and readability of your code. When working with Graphics Designers this also allows an area for pure HTML which can be linked quickly with the logic code behind, allowing PHP coding in a large muti-discipline team.

Views for the application are added into the folder “framework/application/view” and are called from controllers, this simple file can contain any type of code. Let’s start by showing an example.

Type the following code into framework/application/view/index.php

  1. <html>
  2. <head>
  3. <title></title>
  4. </head>
  5. <body>
  6. Hello World!
  7. </body>
  8. </html>

Let’s try and load this in a controller:

  1. namespace Controller;
  2.  
  3. class Home {
  4. public function index() {
  5. \Core\Router::loadView("index");
  6. }
  7. }

You can see that he Router::loadView function actually includes the framework/application/view/index.php (basically navigates to the folder and adds a PHP on the end), and displays it in the controller. You can also have a folder structure in the view folder, for example Router::loadView(“x/y/z/view”) would load framework/application/view/x/y/z/view.php

Variables are easy to pass to views too:

  1. <html>
  2. <head>
  3. <title></title>
  4. </head>
  5. <body>
  6. Hello <?php echo $name ?>!
  7. </body>
  8. </html>

  1. namespace Controller;
  2.  
  3. class Home {
  4. public function index($id="World") {
  5. \Core\Router::loadView("index", array("name"=>$id));
  6. }
  7. }

Now try loading http://localhost/, http://localhost/ivebeenlinuxed or http://localhost/test – Clever eh!

Advantages to using views?

  • Graphics designers add HTML files to the versioning system, without having to deal with large amounts of PHP
  • PHP and HTML are kept separate making code easier to read, and HTML easier to manage.
  • Views can be used in multiple controllers with different variables being passed to them

Add Comment | Categories: Boiler Framework | Posted: February 1, 2012

Leave a Reply

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


9 − = four

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>