Documentation:Views

From TinyMVC Documentation

Jump to: navigation, search

Contents

What is a view?

View files contain the final presentation elements for your content, such as HTML markup and style sheets.

Learning by example

View files live in the views directory. Lets create a view:

/myapp/
  /views/
    hello_view.php

It is recommended, but not required, to append _view to your view file names.
This makes it easier to distinguish view files from other types of files in your editor.

hello_view.php

<html>
  <head><title>Hello</title></head>
  <body>
    Hello World.
  </body>
</html>


To see the view file, we call it from within the controller.

/myapp/controller/hello.php

class Hello_Controller extends TinyMVC_Controller
{
  function index()
  {
    $this->view->display('hello_view');
  }
}

Always provide the view filename without the .php extension.

Load the page in your browser:

http://localhost/index.php/hello

You should see the contents of the hello_view file (view browser source):

<html>
  <head><title>Hello</title></head>
  <body>
    Hello World.
  </body>
</html>

Capture view output to a variable

To assign the output of the view to a variable, use $this->view->fetch().

/myapp/controller/hello.php

class Hello_Controller extends TinyMVC_Controller
{
  function index()
  {
    $output = $this->view->fetch('hello_view');
  }
}

Assigning variables to the view

There are several ways to assign vars to the view. The first method is using assign().

/myapp/controller/hello.php

class Hello_Controller extends TinyMVC_Controller
{
  function index()
  {
    $this->view->assign('title','Hello');
    $this->view->assign('body_text','Hello World.');
    $this->view->display('hello_view');
  }
}

hello_view.php

<html>
  <head><title><?=$title?></title></head>
  <body>
    <?=$body_text?>
  </body>
</html>

You should see exactly the same output, but this time some of the content was assigned (view browser source):

<html>
  <head><title>Hello</title></head>
  <body>
    Hello World.
  </body>
</html>

You can also assign arrays of data instead of key/value:

$data['title'] = 'Hello';
$data['body_text'] = 'Hello world.';
$this->view->assign($data);


When you assign variables with $this->view->assign(), it internally collects the values in the $this->view->view_vars property array, which is automatically extracted to every view.

The variables assigned with $this->view->assign() are always available to every view thereafter. If you want variables assigned to only the current $this->view->display() call, you can pass the vars as the second parameter.

$data['title'] = 'Hello';
$data['body_text'] = 'Hello world.';
$this->view->display('hello_view',$data);


For brevity, this example assigns text directly from the controller. To maintain proper MVC separation, content should come from view files, models (database) or other sources such as config files or libraries. As a rule of thumb, you should never have to edit a controller or model PHP file to change the presentation.

Personal tools