Invenzzia »

How to load templates from a database?

From Invenzzia wiki

Jump to: navigation, search

The templates do not have to be stored in files on your hard drive. With PHP streams, you can keep them anywhere you want:

$tpl->sourceDir = 'phar://file.phar/';

PHP allows you to write custom stream wrappers which can be used to create a new storage engine for your templates. The details of writing a new stream wrapper can be found in PHP manual. Once registered, the wrapper can be used in OPT:

stream_wrapper_register('db', 'DatabaseStreamClass');
 
$tpl->sourceDir = array(
'file' => './templates/',
'db' => 'db://templates/'
);

To access the templates in a new stream, you follow the template name with the stream name:

$view = new Opt_View('db:template_from_db.tpl');

The same trick works in the templates. If the stream wrapper is not specified, OPT uses the stream defined in $tpl->stdStream (file by default).

OPT streams do not have to represent different PHP streams. You can define multiple streams in sourceDir pointing to different places of a filesystem, where the templates are located.

Personal tools