A small, object-oriented PHP library that provides a DOM-like API to build, manage and render xHTML pages programmatically. The library helps separate presentation structure from application logic by exposing HTML elements as PHP objects that can be composed, nested and rendered as compliant xHTML.
Highlights
- Lightweight OO approach to generate xHTML programmatically
- DOM-like tree model with element classes for common HTML tags
- Helpers for tables, unparsed blocks and structured content insertion
- Output is intended to be valid xHTML (e.g., xHTML 1.0 Transitional)
When to use
- You want clear separation between HTML structure and PHP logic
- You prefer building pages by composing elements instead of echoing HTML strings
- You need programmatic helpers for tables, fragments and unparsed HTML
Quick Start
- Include the library (use the
srcfolder or place files in your include path).
require_once 'src/xHTML_All.php';The following example is preserved from the original repository and demonstrates basic usage and features.
<?php
/*******************************************************************/
/* As it can be seen here, it is very easy to split basic */
/* structure (that can be placed in your class constructor) */
/* from page's content, that can be dynamically placed in your */
/* member functions or similar. */
/* Of course, its output is 100% xHTML 1.0 Transitional Valid */
/*******************************************************************/
require_once '/xHTML_All.php';
/** Creating basic object structure and properties **/
$webPage=new xHTML_Page("My testing page");
$webPage->AddCSSFile("some-css-file.css");
$header=new xHTML_Div();
$header->SetID("header");
$footer=new xHTML_Div();
$footer->SetID("footer");
$contentMainDiv=new xHTML_Div();
$contentMainDiv->SetID("content");
/** Adding and "linking" structure items altogether **/
$webPage->AddContent($header);
$webPage->AddContent($footer);
$footer->AddContentBefore($contentMainDiv); //Demonstration of inserting before an already existing element
$contentMainDiv->AddContent(new xHTML_P("Welcome to the xHTML-o-MATIC test page!")); //Demonstration of dynamic content adition when structure have already been stablished
$contentMainDiv->AddContent(new xHTML_UnparsedCode("<p>And also, it allows to add unparsed code by hand like this <a href='http://www.google.es'>google link</a> inside a p element</p>"));
/** Demonstration of "automatic" table handling, to prevent those annoying <tr> and <td> **/
$table=new xHTML_Table();
$table->SetProperty("align", "center"); //Property set demonstration
$table->SetProperty("border", "1");
$table->SetProperty("width", "60%");
$table->AddContentMatrix(0, 0, new xHTML_P("This is cell 0,0"));
$table->AddContentMatrix(0, 1, new xHTML_P("This is cell 0,1"));
$table->AddContentMatrix(1, 1, "This is cell 1,1"); //No p item, just text, and item 1,0 left empty with no problems
$table->SetCellProperty(1, 1, "align", "right"); //Demonstration for an easy cell property set inside its matrix
$webPage->AddContent($table);
$webPage->Render(); //Renders (outputs) the whole configured page
?>Available classes
- Core include:
src/xHTML_All.phpto require all element classes - Element classes included in the library (found in
src/):W3COKButton.phpxHTML_A.phpxHTML_BR.phpxHTML_Base.phpxHTML_Body.phpxHTML_Comment.phpxHTML_Div.phpxHTML_Exceptions.phpxHTML_Form.phpxHTML_Generic.phpxHTML_H.phpxHTML_Head.phpxHTML_Img.phpxHTML_Link.phpxHTML_List.phpxHTML_P.phpxHTML_Page.phpxHTML_Script.phpxHTML_Span.phpxHTML_Style.phpxHTML_Table.phpxHTML_Text.phpxHTML_UnparsedCode.php
Documentation
- Full generated documentation is available in the
docs/folder. Opendocs/index.htmlin a browser to browse the API and examples.
Installation and integration
- There is no installer; copy the
src/folder into your project andrequire_once 'src/xHTML_All.php'from your bootstrap or script. - The library is plain PHP and has no external dependencies.
Contributing
- Contributions are welcome. Please open issues or pull requests with clear descriptions and tests where appropriate.
License
- This project is licensed under the GNU General Public License v3.0. See the
LICENSEfile for details.
Support
- For questions or issues, please open an issue in the repository.