Views


Description


Views are normal PHP files that contain the HTML, XML, etc. that you want your application to display. A view can be an entire web page or just a part of a page like a header, menu or footer.

Using views allows you to separate the presentation and business logic of your application, permitting independent development, testing and maintenance of each.

All views must be located in the app/views directory. You can also create subdirectores to better organize your view files.

Note that the default content type header sent by the Mako Framework is Content-Type: text/html; charset=UTF-8. You can easily overwrite the default header in your controllers before displaying your views (e.g., "Content-Type: text/xml; charset=UTF-8" for XML content).


Methods


__construct(string $view [, array $variables = array()])


You create a view object by passing the name of the view file to the view constructor.


// Loads the welcome.php view

$view = new View('welcome');

// Loads the bar.php view located in the foo directory

$view = new View('foo/bar');

// Loads the welcome.php view and assigns the "foo" variable with the value "bar"

$view = new View('welcome', array('foo' => 'bar'));

factory(string $view [, array $variables = array()])


The factory method returns a view object. This allows you to chain methods.


$output = View::factory('welcome')->assign('foo', 'bar')->render();

assign(string $key, mixed $value [, boolean $global = false])


Assigning variables is done by using the assign medhod of the view class. You can assign any kind of variable, even another view object. The assigned variable is only available in the view you assigned it to. To make a view variable available in all views then pass the value TRUE as the third parameter.


// Assigned the variable $foo with the value "bar"

$view->assign('foo', 'bar');

// Assigned the variable $foo with the value "bar" and made it available in all views

$view->assign('foo', 'bar', true);

// You can also assign variables using overloading but you will not be able to make them global

$view->foo = 'bar';

assignByRef(string $key, mixed $value [, boolean $global = false])


The assignByRef method works just like the assign method except that it assigns variables by reference.


$view->assignByRef('foo', $bar); // The reference to $bar is now assigned to $foo

render([callback $filter = null])


The render method returns the rendered output of the view. You can pass a callback function or closure to filter the output.


// Returns the rendered output

$output = $view->render();

// The view class implements the __toString method can just cast the view object to a string to render it

$output = (string) $view;

// Returns the rendered output after converting it to uppercase

$output = $view->render('mb_strtoupper');

// Returns the rendered output after making the first character of each word in capitalized

$output = $view->render(function($output)
{
	return mb_convert_case(mb_strtolower($output), MB_CASE_TITLE);	
});