
Tag Library Engine for PHP 5
|
|
IntroductionWhat it isPhable is a small and fast Taglib Engine for PHP. It's purpose is similiar to that of Template Engines like Smarty but instead of introducing special markup Phable uses prefixed tags known from XML for it's special functions. Basically Phable was inspired by the taglibs from Sun's JavaServer Pages and especially by Sun's JSP Standard Taglib Library. Phable tries to be as compatible as possible to the JSTL but will differ where it makes sense. How it looksHere is short example which does a simple iteration over a comma separated list of fixed values:
<?php require_once 'Phable/Controller.php' ?>
<%@ taglib file="Phable/taglibs/STL/Core" name="STL_Core" prefix="c" %>
<%@ taglib file="Phable/taglibs/STL/Functions" name="STL_Functions" prefix="fn" %>
<html>
<body>
<ul>
<c:forEach items="Mercury,Venus,Earth,Mars" var="planet">
<li>${fn:toUpperCase(planet)}</li>
</c:forEach>
</ul>
</body>
</html>
The first line is the only PHP code you will need to use Phable. This line includes the on-thy-fly controller of Phable which will automatically start the Taglib Engine and passes the current PHP script to it. This is only needed if you want to call your pages directly from the browser. If you use a MVC approach instead then you don't need this line because then you have your own controller which can start the Taglib Engine and tells this engine to draw a specific page. The next two lines are taglib definitions. They tell the engine to load the taglibs STL_Core and STL_Functions from the specified PHP files and bind these taglibs to the prefixes c and fn. These taglibs are part of the Standard Tag Library which comes with Phable. c:forEach is a tag of the STL_Core taglib. It iterates over the specified items and stores the current value of the iteration in the page scope variable planet. Inside the iteration a taglib expression is used. Expressions are using the special markup ${...}. Normally these expressions are only used inside of tag attributes but you can also use them anywhere in the HTML code if you like. In this case the expression is used to print the value of the iteration variable after it is converted to upper case letters. The expression function toUpperCase comes from the Function-taglib STL_Functions. How it worksPhable consists of two main parts. The first part is the Engine. This is just a small PHP include file which must be included on each request. The engine is responsible for drawing a taglib enabled page. The second part is the Compiler. If the Engine encounters a page which is not compiled yet then the Engine loads the Compiler (which is another PHP include file, somewhat larger as the Engine but still very small) and tells the Compiler to compile the page. The Compiler now reads the page and converts all the special markup into real PHP code by using Perl-compatible regular expressions. The result is cached so the next time the page is requested the engine can directly call the compiled page. With this approach your pages are still very fast. What taglibs arePhable knows three types of taglibs. The simplest taglib type is the Runtime Taglib. This is just a simple include file holding a class where each method represents a tag. The Compiler converts calls to these tag methods into simple PHP method calls while the taglib definition is converted into a simple PHP include line which loads the taglib during runtime. The second taglib type is the Compile-time Taglib. This one is only loaded during Compile-time but it also just contains a class with methods where each method represents a tag. But these methods don't return HTML content like the methods in a Runtime Taglib. Instead these methods return PHP code which is inserted into the compiled page. So using compile-time taglibs results in faster code and some functionality (like iterations) must be implemented in compile-time taglibs, The third taglib type is the Function Taglib which is not really a taglib. This taglib type does not define tags but it defines expression functions. These functions are organized as static methods in a simple class. Function taglibs are handled in the same way as runtime taglibs. Expression function calls are converted to PHP method calls by the compiler and the function taglib is included during runtime. RequirementsPhable makes extensive use of the new object oriented features of PHP 5. So you need at least PHP 5.0 to use Phable. It's not possible to use Phable with PHP 4 and more antic versions. |