Open Source Training Seminar FreePBX Paid Support

Views

Part of the ApiDocs

Status

Currently, views are only in the [browse:freepbx/branches/quickform quickform branch]. (First implemented in [3579])

They work in the base admin directory, but are not yet implemented for modules (eg, so modules can provide and use their own set of views).

Overview

Views are a concept from the MVC pattern. Basically a view is just the code to create the HTML output, which allows it to be separate from the rest of the code. In freePBX's case, there is no model, but the config.php file does act like the controller.

API

loadview($viewname, $parameters = false)Load a view, returning the output
showview($viewname, $parameters = false)Display the output from a view

$viewname is the name of the file in the views/ directory (without .php). $parameters is an (optional) associative array that will be extracted to regular variables inside the view. Passing array('foo'=>'bar') for example will make $foo available inside the view, with the value 'bar'.

Creating Views

A view is simply a regular PHP file that has HTML output. It can use variables passed with the parameters parameter (described above), but it should not assume that any exist.

Though it is not technically necessary, it is highly recommended that a single view contain a full "block" of HTML - that is to say, it should contain all the complementary tags. As opposed to having seperate "header" and "footer" views, a single view should be created that has eg, <?echo $content; ?> in the middle.

Using Views

Basic use of views is fairly simple, simply call either loadview() or showview() with the name of the view and any parameters.

Stacking Views

There are a few different ways to combine multiple views. One of them is to "stack" them using content, for example:

template.php:

<html>
<title><?php echo $title; ?></title>
<body>
<div id="header"><h3>Title</h3></div>
<div id="content">
  <?php echo $content; ?>
</div>
</body>
</html>

welcome.php:

<p>Hi, <?php echo $name; ?>, welcome to freePBX!</p>
<ul>
  <li>The great new view system</li>
  <li>Notice this file contains all the complementary closing tags for any tags it opened.</li>
</ul>

Putting them together:

$template['content'] = loadview('welcome', array('name'=>'John Doe'));
$template['title'] = 'freePBX Views Test';
showview('template', $template);

Embedding Views in other Views

Using the same template as above, we could load it with the following view:

welcome2.php:

$template['content'] = "<p>Hi, ".$name.", welcome to freePBX!</p>".
                       "<ul>".
                       "  <li>The great new view system</li>".
                       "  <li>Notice this file contains all the complementary closing tags for any tags it opened.</li>".
                       "</ul>";
$template['title'] = $title;
showview('template', $template);

And use this in the controller as follows:

showview('welcome2', array('title' => 'freePBX views test', 'name'=>'John Doe');

Both of these methods will contain the same output, but generally embedding is better for simplistic pages (eg, 404 pages) where you don't want to set up the parameters array in the controller, and don't have a lot of content to output.

Donate



Support
Download
Develop
Forums
News
Documentation
Paid Support
About

Paid Ads