Writing taglibs is straighforward, it is important to understand and remember the difference between taglibs and function taglibs. Function taglibs are PHP functions that are wrapped in a class and accessed under a common taglibs prefix. These functions can be called in the expression language.
Plain taglibs are also PHP function wrapped in a PHP class, each function defines a new tag that can be used in your document. The function gets called anytime the parser encounters the corresponding tag in the document. This means that for a bodyless tag the function is called only once, for tags with body it is instead called twice: once when the tag is open and once when it is closed.
To write a new function taglib create a new PHP file in a directory place it in a directory from the tree Phable/taglibs/ (you can create a new directory or place the PHP file in an existing one), this is where the other taglibs files are placed. Then wrap your function in a class, prefixing the class name with Phable_FT_:
Example 2.10. Writing a function taglib
<?php
require_once(MWT_ABS_DIR . "Phable/Function_Taglib.php");
class Phable_FT_MyFunctions extends Phable_Function_Taglib
{
public static function func1($arg1) {
...
}
public static function func2($arg1, $arg2) {
...
}
}
?>
The above example shows the implementation of a function taglib containing two functions called func1 and func2. In order to use taglibs you need to import them in your pages, this is explained in paragraph Importing taglibs in pages.
To write a taglib you need to do the following:
The next example shows the implementation of a taglib that defines a bodyless tag and a tag with body. In the example, the bodyless tag requires one attribute and can only be used inside the tag with body.
Example 2.11. Writing a taglib that implements a bodyless tag and a tag with body
<?php
require_once(MWT_ABS_DIR . "Phable/Compiletime_Taglib.php");
class Phable_CT_MyTaglib extends Phable_Compiletime_Taglib
{
public function tag_bodyless() {
$this->requireParent('withbody');
$this->rejectBody();
$this->acceptAttribs('attrib1');
$this->requireAttribs('attrib1');
//output taglib code
.....
}
public function tag_withbody() {
$this->requireBody();
$this->rejectAttribs();
if ($this->type == PHABLE_TAG_OPEN)
{
//output opening code
.....
} else {
//output closing code
.....
}
}
?>
Taglibs can be imported in two ways:
If you are going to use the taglib in only a few pages then choose the second option, as the taglib engine performs faster when less taglibs are included. The example below shows how to import the example taglibs shown in the previous paragraphs in a page:
Example 2.12. Importing taglibs and function taglibs in pages
..... <layout name="mypage"> <widgets> <widget name=".."/> ... </widgets> <taglibs> <taglib file="taglibs/MYDIR/MyFunctions.php" name="MyFunctions" prefix="myfuncs"/> <taglib file="taglibs/MYDIR/MyTaglibs.php" name="MyTaglib" prefix="mytags"/> </taglibs> </layout> ...
After the taglibs have been imported in a page, the tags can be used in all its internal widgets.
The next example shows how to use the taglibs defined above in a widget:
Example 2.13. Using your taglibs in pages
<mytags:withbody>
<mytags:bodyless attrib1="${myfuncs:func1()}" />
</mytags:withbody>