Views API
Part of the ApiDocs
Overview
Views are a way of doing templates, borrowed from the MVC pattern. In FreePBX they are basically very simple PHP pages, that use normal PHP code, and run in a limited scope with their own variables.
Usage
There are two functions:
showview($viewname, $parameters = false)
loadview($viewname, $parameters = false)
They are effectively the same, the only difference being that showview() outputs the result directly, eg:
showview('someview');
while loadview() returns the result:
$output = loadview('someview');
$viewname is the name of the view, and there should be a file views/$viewname.php (note that $viewname should not include the ".php") $parameters is an optional associative array of parameters to pass. The key value will be passed into the view as a variable name.
Examples
Simple view
<?php $params = array( 'text' => 'hello world', 'title' => 'Example view', ); showview('helloworld', $params); ?>
views/helloworld.php:
<html> <head> <title><?php echo $title; ?></title> </head> <body> <div id="text"> <?php echo $text; ?> </div> </body> </html>
Multiple views
The idea here is that views are cascaded to build a complete page. Each view actually contains a complete block of HTML, in that each element that is opened, is closed in the same file. This eases maintenance because it is easy to find/add corresponding element closures, so it is highly recommended to set up views this way.
<?php $user_data = array( 'name' => 'John doe', 'age' => '25', ); $main_template = array(); $main_template['title'] = 'User details'; $main_template['content'] = loadview('userinfo', $user_data); echo loadview('template', $main_template); ?>
views/template.php:
<html> <head> <title><?php echo $title; ?></title> </head> <body> <div id="page"> <?php echo $content; ?> </div> </body> </html>
views/userinfo.php:
<div id="userinfo">
<h2> User information </h2>
<div id="username">
Username: <?php echo $name; ?>
</div>
<div id="age">
Age: <?php echo $age; ?>
</div>
</div>
