Skip to content
mattbasta edited this page Aug 13, 2012 · 5 revisions

Interchange employs inherited views and view constants to render client output. Inherited views allow a view to render one or more views within itself, rendering the contents of one view inline into another.

Views are managed using the static class view_manager.

Adding and Rendering Views

To add a view, use add_view function:

view_manager::add_view("shell");
view_manager::add_view("body");
view_manager::add_view("details");

Views should be added in order of outermost to innermost. Consequently, “shell” views (views that render required HTML elements) should be placed before content views. In the example above, the “shell” view would wrap the “body” view, which wraps the “details” view.

In a more advanced configuration, “shell” could wrap both “body” and “details”, where “body” and “details” are adjacent. This, however, can cause difficulties with rendering views within “body” and “details”, requiring complicated ordering.

Rendering views is accomplished using the render function:

echo view_manager::render();

// or

$rendered_views = view_manager::render();
echo htmlentities($rendered_views);

Each view should also call this method (and somehow echo the value) to render the next view in the stack. For instance, a shell view might look like this:

<!DOCTYPE html>
<html>
<head>
    <title><?php echo view_manager::get("TITLE"); ?></title>
</head>
<body><?php echo view_manager::render(); ?></body>
</html>

The next view in the stack would be rendered at the render statement.

View Values

A value within a view is used to pass information into a view. These values can be retrieved and set at any time, by any object or function (even from within views).

Getting Values

There is a simple getter for view values:

echo view_manager::get("VALUE_NAME");

It is recommended that value names are written in all caps. It should be noted that value names are not escaped or otherwise processed.

If a value is requested that does not exist, false is returned.

Setting Values

Setting values is done via a simple mutator:

view_manager::set("VALUE_NAME", "value value");

As mentioned above, this function can be called universally from Interchange. The type of the value being set is preserved.

Shortcuts

As a matter of convenience, certain functions have been abbreviated and combined to both simplify code and improve readability. The following is a listing of all of the functions available through the views interface.

Function Call Function
render_as_value($name) view_manager::set_value($name, view_manager::render())
render_as_httpresponse() new HttpResponse(view_manager::render())
render_and_pipe($through) pipe::through(view_manager::render(), $through)
dump() Clears the render stack

Clone this wiki locally