The first step in creating a custom tag is to create a file in the tag search path with the extension .tag.php. Any files with this extension in the compiler search path will automatically be loaded by the compiler.
This file must register its tag class using by creating an instance of the TagInfo Class. For example:
/** * Register the tag */ $taginfo =& new TagInfo('core:INCLUDE', 'CoreIncludeTag'); $taginfo->setEndTag(ENDTAG_FORBIDDEN); $taginfo->setCompilerAttributes(array('file', 'literal'));
The first argument, Tag represents the name of the tag.
setEndTag tells the compiler how to process tags with contents. Allowable values are:
| ENDTAG_REQUIRED | Closing tag is required. |
|---|---|
| ENDTAG_OPTIONAL | not implented yet. Treated the same as ENDTAG_REQUIRED for now. |
| ENDTAG_FORBIDDEN | No closing tag is allowed. |
TagClass tells the compiler which tag class to instantiate when this tag is used in a template.
The .tag.php file should register the tags that it contains with the compiler using the TagDictionary::registerTag function. for example:
TagDictionary::registerTag($taginfo, __FILE__);
When creating a tag class, you can inherit from one of four base classes:
| SilentCompilerDirective Tag | Silent compiler directive tags are instructions for the compiler and do not have corresponding Components, nor do they normally generate output into the compiled template. |
|---|---|
| CompilerDirective Tag | Compiler directive tags do not have corresponding server Components, but they do render their contents into the compiled template. |
| ServerComponent Tag | Server component tags have a corresponding server component which represents an API which can be used to manipulate the marked up portion of the template. |
| ServerTagComponent Tag | Server tag component tags are ServerComponent Tags which also correspond to an HTML tag. |
Here is an example tag class definition:
class CoreIncludeTag extends CompilerDirective { }
After defining your class, you can now override the appropriate TemplateMethods to provide the functionality of your tag. See CodeWriter for information on how to generate code for a template.
function preGenerate(&$code)
Generate Template code before the contents of your tag.
function generateContents(&$code)
Generate Template for the contents of your tag.
function postGenerate(&$code)
Generate Template code after the contents of your tag.
function CheckNestingLevel
Called by the parser to check for proper usage and nesting.
function preParse
The result of this method tells the parser whether to parse the contents of this tag. Allowable return values are:
function prepare
This method is called on your tag when the entire document tree has been parsed. This is an opportunity to manipulate the tree or initialize variables before code generation begins.
The following methods are only used for ServerComponent Tags and ServerTagComponent Tags:
function generateConstructor(&$code)
Generate code in this method which is used to construct the Component which corresponds to your class
function generateExtraAttributes(&$code)
function getRenderedTag
Override the following two methods to make your tag a DataSource:
function &getDataSource()
function getDataSourceRefCode
Some basic functionality for debugging templates as you develop them is provided by the Dev Source Tag and the Dev Tree Tag.
For further information see Developing_A_Calendar_Component_Tag and Developing_A_Data_Table_Component_Tag.