lime icon

Phosphorus and Lime

A Developer's Broadsheet

This blog has been deprecated. Please visit my new blog at klenwell.com/press.
PEAR: templates and renderers
PEAR Flexy documentation

Trying to figure out how to use renderer with PEAR QuickForm class. Hard to believe that this is supposed to make development simpler or more efficient than patching together your own pages just using PHP and HTML. But I figure it's a lesson in PHP classes if nothing else.

Focusing on Flexy, which is described as follows

the long term aim of Flexy is to provide a universal Template Base API for Compiling and native PHP type templates


Basic routine

# create template (separate file)

# include class (e.g. HTML/QuickForm/Renderer/Object.php)

# instantiate renderer class
$Renderer =& new HTML_QuickForm_Renderer_Object(TRUE);

# assign classes to elements
$_CSS['CLASS'] = array
(
'text' => 'text',
'textarea' => 'textarea',
);

$Renderer->setElementStyle($_CSS['CLASS']);

# give renderer to form class
$Qform->accept($Renderer);

# instantiate template class

# configure options (as static)
$TPL_OPTIONS = &PEAR::getStaticProperty('HTML_Template_Flexy','TPL_OPTIONS');
$TPL_OPTIONS = array
(
'templateDir' => './templates',
'compileDir' => './templates/build',
'debug' => 0
);

# instantiate class
$Template =& new HTML_Template_Flexy($TPL_OPTIONS);

# instantiate "Convenience class for the form object passed to outputObject()" (demo comments)
# I don't fully comprehend the function of this step yet
$View = new StdClass; # generic class
$View->form = $Renderer->toObject(); # returns convenience class as object

# compile
$Template ->compile('flexy-dynamic.html');

# display
$Template ->outputObject($View);


As noted in comments above, I still don't fully understand the logic of the generic $View class. I can follow the processing but don't see why, after compiling the template, you wouldn't use some method like $Template->outputHTML() to display the template. Also, I'm a little confused as to why the entire object $View is passed to outputObject() and not $View->form, the variable to which the result of $Renderer->toObject() was assigned.

I did find this page with a little more info on Renderers. It notes:

Renderers are plugins that can transform the form that you have defined into any format supported.


So to conclude, this examples involves three different objects:

1. Renderer (accepted by QuickForm object $Qform)
$Renderer =& new HTML_QuickForm_Renderer_Object(TRUE);

2. Template (which compiles template and outputs outputs form)
$Template =& new HTML_Template_Flexy($TPL_OPTIONS);

3. Viewer (which represents Renderer as object)
$View = new StdClass;
$View->form = $Renderer->toObject();

I've not gotten into the template itself yet ('flexy-dynamic.html'). Tomorrow perhaps.
Found this semi-tutorial on Flexy:

http://metapundit.net/samples/flexy/

As the author notes, Flexy documentation leaves something to be desired.