Display

Phork comes with a CoreDisplay class used to pull together the various parts of a page. The controller is generally the only code that should add output to the display class.

The display class handles output buffering which allows content to be rearranged and replaced after it has been added to the display object. If output buffering has been turned on then the page will output when the output() method is called manually or the display object has been destroyed and the __destruct() method has been called. If output buffering is off then the content is output as soon as it's added to the display object.

The display class is a singleton meaning there can only be one instance of it at a time.

Headers

Custom headers such as location redirects can also be added with appendHeader(). In order for a header to be sent it must be appended before any output has been displayed.

AppDisplay::getInstance()->appendHeader('location: /');

Status Codes

Custom HTTP status codes (eg. 404 page not found) can be set with setStatusCode(). AppDisplay::getInstance()->setStatusCode(404);

Display Nodes

The display class works with nodes. A page can be made up of one or more nodes. For example, you might want a header node, a content node, and a footer node. Or you may just want a single node that includes the header and footer content in it.

If you have multiple nodes with output buffering turned on then you can rearrange the nodes. This is helpful when, for example, you need to display all errors at the top of the page, even if an error was generated somewhere in the middle of the page. In that case you would append the error node last, and use setNodeOrder() to output it in the right place.

Node Types

A node can be added in one of two ways. It can be a template that's included with appendTemplate(), or it can be a string that's included with appendString().

Variables in Templates

If there are any variables needed within the template, a second argument can be passed to appendTemplate() with an array of variables to be extracted for use within the template. The key of the array items will be used as the variable name when the data has been extracted.

Compression

If the zlib extension is available, the display class can turn on compression using setCompress().

Extending the Display

The default class is AppDisplay which is an empty extension of CoreDisplay. Any changes that you need to the display should be done in AppDisplay.


Node Example (from within a controller)

AppDisplay::getInstance()->appendTemplate('header', $this->getTemplatePath('common/header'));
AppDisplay::getInstance()->appendTemplate('content', $this->getTemplatePath('index'), array(
    'strFoo' => $strFoo,
    'intBar' => $intBar
));
AppDisplay::getInstance()->appendTemplate('footer', $this->getTemplatePath('common/footer'));

404 Page Example (from within a controller)

AppDisplay::getInstance()->setStatusCode(404);
AppDisplay::getInstance()->appendTemplate('error', $this->getTemplatePath('system/404'));

String Example (from within a controller)

AppDisplay::getInstance()->appendString('content', 'This will be output on the page!');

Code Replacement Example

//replace the load time placeholder in the built page
$objDisplay = AppDisplay::getInstance();
$objDisplay->replace('<[LOAD TIME]>', $strLoadTime);