Table of Contents

Views & Templates

In Bedrock, views are classes that contain all logic related to the user interface of an application. Like Bedrock controllers, views make use of a nested hierarchy structure to minimize code redundancy and improve maintainability.

Definition & Structure

Each view class represents a container for a specific part of an application's user interface. All view classes are extended from the base class Bedrock::View which provides some common functionality across all view objects (method implementations, member variables, and docblocks have been omitted for brevity):

  1. abstract class Bedrock_View extends Bedrock {
  2.  
  3. public function __construct() {...}
  4.  
  5. public function setValue($name, $value, $default = '') {...}
  6.  
  7. public function getValue($name) {...}
  8.  
  9. public function __set($name, $value) {...}
  10.  
  11. public function __get($name) {...}
  12.  
  13. public function printValue($name) {...}
  14.  
  15. public function setForm($name, $form) {...}
  16.  
  17. public function getForm($name) {...}
  18.  
  19. public function printRoot($which) {...}
  20.  
  21. public static function registerJavascriptTemplate($javascriptTemplate) {...}
  22.  
  23. public static function registerJavascriptLibrary($javascriptLibrary) {...}
  24.  
  25. public static function importJavascriptTemplates($javascriptTemplates) {...}
  26.  
  27. public static function importJavascriptLibraries($javascriptLibraries) {...}
  28.  
  29. public static function exportJavascriptTemplates() {...}
  30.  
  31. public static function exportJavascriptLibraries() {...}
  32.  
  33. public static function isAjaxRequest() {...}
  34.  
  35. abstract function render();
  36. }

Storing & Retrieving Data

One of the primary roles view objects have is that of an information container for templates. All data that will be displayed in the browser is assigned to view objects from within controllers. There are five methods used to accomplish this. The View::setValue() and View::getValue() methods as well as the __set() and __get() magic methods are used to store and retrieve data within a view object.

The magic methods allow data stored in view objects to be accessed and manipulated like member variables. This is the most common method for storing and retrieving data in view objects. In addition to these methods, View::setValue() and View::getValue() allow for default values to be used when a requested/specified value does not exist.

Consider the many ways a value can be stored:

  1. // Assigns the string "bar" to the value "foo"
  2. $viewObj->foo = 'bar';
  3.  
  4. // The above code is translated to this:
  5. $viewObj->__set('foo', 'bar');
  6.  
  7. // Assigns the string "bar" to the value "foo"
  8. $viewObj->setValue('foo', 'bar');
  9.  
  10. // Sets to "bar" because assigned value is empty.
  11. $viewObj->setValue('foo', '', 'bar');

As well as the many ways a value can be retrieved:

  1. // Outputs the string "bar" for the value "foo"
  2. echo $viewObj->foo;
  3.  
  4. // The above code is translated to this:
  5. echo $viewObj->__get('foo');
  6.  
  7. // Outputs the string "bar" for the value "foo"
  8. echo $viewObj->getValue('foo');
  9.  
  10. // Returns the default 'bar' because 'blah' does not exist.
  11. echo $viewObj->getValue('blah', 'bar');

Rendering

Because views represent visual components for an application, they must be rendered in order to be displayed on a page. This is done through a View::render() method that is defined abstractly in the base class. When implementing this method, all code required to produce output to the browser should be executed here. Lets look at a simple example:

  1. class Application_View_Links extends Bedrock_View {
  2. /**
  3.   * <... docblock ...>
  4.   */
  5. public function __construct() {
  6. parent::__construct();
  7. }
  8.  
  9. /**
  10.   * <... docblock ...>
  11.   */
  12. public function render() {
  13. echo '<a href="' . $this->getvalue('link_01') . '">Link 1</a>';
  14. echo '<a href="' . $this->getValue('link_02') . '">Link 2</a>';
  15. echo '<a href="' . $this->getvalue('link_03') . '">Link 3</a>';
  16. }
  17. }

Then, in a controller method, you could write this:

  1. //...
  2.  
  3. // Initialize View
  4. $links = new Application_View_Links();
  5.  
  6. // Assign Values
  7. $links->link_01 = 'http://www.bedrockframework.com/';
  8. $links->link_02 = 'http://www.php.net/';
  9. $links->link_03 = 'http://www.google.com/';
  10.  
  11. // Render View
  12. $links->render();
  13.  
  14. //...

Which would output this to the browser:

  1. <a href="http://www.bedrockframework.com/">Link 1</a>
  2. <a href="http://www.php.net/">Link 2</a>
  3. <a href="http://www.google.com/">Link 3</a>

JavaScript Support

Because views represent everything that is sent to the browser, they also provide support for managing JavaScript code. Bedrock manages JavaScript code in two forms: libraries and templates. Any JavaScript code stored in their own source files (*.js) are considered libraries. Any JavaScript code that is built dynamically or includes PHP code in some form is considered a JavaScript template.

JavaScript Libraries

JavaScript Templates

Nesting & Hierarchy

Templates

Bedrock templates represent a collection of files that produce a common look and feel for a Bedrock application. A typical template will consist of images, CSS files, and PHP scripts stored in own directory within the application's template root directory (located here by default: ”\application_root\pub\tpl”). In cases where no template is defined, or where a particular component is not supported by the active template, a built-in default template is used to display content.

For more details about the structure of a template and to learn how to develop your own, see the Templates subsection within the Extending Bedrock section.