LogoPhly, boy, phly
the weblog and site of Matthew Weier O'Phinney

Monday, June 5. 2006

Cgiapp2 Tutorial 1: Switch Template Plugins at Will

This is the first in a short series of tutorials showcasing some of the new features of Cgiapp2. In this tutorial, you will see how easy it is to switch template engines in Cgiapp2-based applications.

Cgiapp2 implements a new callback hook system, which is basically an Observer pattern. Cgiapp2 has a number of registered hooks to which observers can attach; when a hook is triggered, each observer attached to it is notified and executed. Additionally, Cgiapp2 provides a means to create new hooks in your applications that others may observer; that's a subject for another post.

Why all this talk about hooks? Because in Cgiapp2, the various template actions -- initialization, variable assignment, and rendering -- are relegated to hooks. For simplicity's sake, and for backward compatibility, you can use the functions tmpl_path(), tmpl_assign(), and load_tmpl() to invoke them; you could also use the generic call_hook() method to do so, passing the hook name as the first argument.

To standardize template actions, I developed Cgiapp2_Plugin_Template_Interface, a standard interface for template plugins. Any template plugin that implements this interface can be called with the standard tmpl_* methods -- which means that developers can mix-and-match template engines at will!

Since Cgiapp2 and its subclasses no longer need to be aware of the rendering engine, developers that are instantiating Cgiapp2-based applications can choose their own rendering engine at the time of instantiation:


<?php
require_once 'Some/Cgiapp2/Application.php';
require_once 'Cgiapp2/Plugin/Savant3.php';
$app = new Some_Cgiapp2_Application($options);
$app->run();
 

In the example above, developer X uses Savant3 as the template engine. Now, say you're developer Y, and have an affinity for Smarty, and want to use that engine for the application. No problem:


<?php
require_once 'Some/Cgiapp2/Application.php';
require_once 'Cgiapp2/Plugin/Smarty.php';
$app = new Some_Cgiapp2_Application($options);
$app->run();
 

Now all you have to do is create Smarty versions of the templates. Cgiapp2 doesn't care to which engine it's rendering; it simply notifies the last registered template plugin.

Stay tuned for more tutorials in the coming days!

Posted by Matthew Weier O'Phinney in PHP at 22:32 | Comments (0) | Trackback (1)

Trackbacks
Trackback specific URI for this entry

Matthew O'Phinney's Blog: Cgiapp2 Tutorial 1 - Switch Template Plugins at Will
Right on the tails of a new release of the Cgiapp set of libraries, Matthew O'Phinney has posted a first tutorial in a series covering its usage. In this tutorial, he looks at a creating a "template switcher" for Cgiapp2-based applications.
Weblog: The PHP Grind
Tracked: Feb 29, 13:48

Comments
Display comments as (Linear | Threaded)

No comments

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 
  • Home
  • Resume
  • Blog
  • Phly PEAR Channel
  • Twitter
  • Contact Me
  • About this site

ZCE

Zend Education Advisory Board Member

Add to Technorati Favorites

Calendar

Back September '10
Mon Tue Wed Thu Fri Sat Sun
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      

Quicksearch

Links

  • PHLY - PHp LibrarY
  • Planet PHP
  • Zend Framework, where I'm project lead
  • Sebastian Bergmann
  • Cal Evans
  • Shahar Evron
  • Paul M. Jones
  • Bill Karwin
  • Mike Naberezny
  • Fabien Potencier
  • Ben Ramsey
  • Derick Rethans
  • Ralph Schindler
  • Marco Tabini

Archives

September 2010
August 2010
July 2010
Recent...
Older...

Categories

XML Linux
XML Personal
XML Aikido
XML Family
XML Programming
XML Dojo
XML Perl
XML PHP

All categories

Syndicate This Blog

XML RSS 0.91 feed
XML RSS 1.0 feed
XML RSS 2.0 feed
ATOM/XML ATOM 0.3 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Comments

Show tagged entries

xml apache
xml best practices
xml books
xml conferences
xml cw09
xml decorators
xml dojo
xml dpc08
xml file_fortune
xml git
xml linux
xml mvc
xml oop
xml pear
xml perl
xml personal
xml php
xml phpworks08
xml programming
xml rest
xml ubuntu
xml vim
xml webinar
xml zendcon
xml zendcon08
xml zendcon09
xml zend framework
© 2004 - present, Matthew Weier O'Phinney
matthew-web <at> weierophinney.net