Hi!
I'm trying to develop a simple and useful template engine, not another computer language, and I'm needing your contribution to improve it, specially for this tasks:
- Implement(permit) HTML in tags values.
- Implement(permit) HTML inside logic tags.
- Implement op(Operator) in logic tags.
Contributions are welcome!
Advises are welcome!
Suggestions are welcome!
Critics are welcome too! đŸ™‚
Note that class is a Beta version, please did not use it in your production scripts because there are no warranty for it. Bugs and functionality issues are known. You'll need php with dom-xml enabled to play with it.
Here's the class: dom_tple.class.php
<?php
/**
* DOM Template Engine 0.1 Beta
*
* A simple PHP DOM-XML based template engine class for separating layout and php.
*
* @author Marco Aurélio R. da Silva
* @copyright Marco Aurélio R. da Silva
* @version 0.1 Beta
* @licence LGPL
*
* Todo:
* - Implement HTML in tags values.
* - Implement(permit) HTML inside logic tags.
* - Implement elseif logic tags.
* - Implement op(Operator) in logic tags.
* - Automatic conversion of HTML templates to XML or XHTML.
*
* NOTE: Contributions are welcome!
* */
class dom_tple {
var $tpl_tags = array(); // where assigned template tags are kept.
/*======================================================================*\
Function: assign()
Purpose: assigns values to template tags
\*======================================================================*/
function assign($tpl_tag, $value=null){
if(is_array($tpl_tag)){
foreach ($tpl_tag as $tag => $val){
if ($tag != '' && isset($val)){
$this->tpl_tags[$tag] = $val;
}
}
}
else{
if($tpl_tag != '' && isset($value)){
$this->tpl_tags[$tpl_tag] = $value;
}
}
}
/*======================================================================*\
Function: parse_tags()
Purpose: parses common tags from template
\*======================================================================*/
function parse_tags($tpl){
foreach($this->tpl_tags as $tag => $val){
$nodes = $tpl->get_elements_by_tagname($tag);
foreach($nodes as $node){
$tag_content = $this->create_temp_tag($tpl, $val);
$node->replace_node($tag_content);
}
}
}
/*======================================================================*\
Function: parse_logic_tags()
Purpose: parses logic tags from template
\*======================================================================*/
function parse_logic_tags($tpl){
$if_tags = $tpl->get_elements_by_tagname('if');
//$elseif_tags = $tpl->get_elements_by_tagname('esleif'); // future implementation.
$else_tags = $tpl->get_elements_by_tagname('else');
if($if_tags){
foreach($if_tags as $if_tag){
$tag = $if_tag->get_attribute_node('tag');
$op = $if_tag->get_attribute_node('op');
$value = $if_tag->get_attribute_node('value');
if($this->tpl_tags[$tag->value] == $value->value){ // how to use $op variable here?
$res = $if_tag->child_nodes();
$tag_val = $res[1]->get_content(); // for revision ... in test.
$tag_content = $this->create_temp_tag($tpl, $tag_val);
$if_tag->replace_node($tag_content);
$if_tag->unlink_node();
}
else{
if($else_tags){
foreach($else_tags as $else_tag){
$res = $else_tag->child_nodes();
$tag_val = $res[0]->get_content();
$tag_content = $this->create_temp_tag($tpl, $tag_val);
$if_tag->replace_node($tag_content);
}
}
}
}
}
}
/*======================================================================*\
Function: create_temp_tag()
Purpose: creates temporary template tags
\*======================================================================*/
function create_temp_tag($tpl, $tag_content){
$temp_tag = $tpl->create_element('temp:tag');
$temp_tag_content = $tpl->create_text_node($tag_content);
return $temp_tag->append_child($temp_tag_content);
}
/*======================================================================*\
Function: remove_temp_tags()
Purpose: removes all temporary template tags
\*======================================================================*/
function remove_temp_tags($tpl){
$tpl = str_replace('<temp:tag>', '', $tpl);
$clean_tpl = str_replace('</temp:tag>', '', $tpl);
return $clean_tpl;
}
/*======================================================================*\
Function: fetch()
Purpose: returns or displays the template output
\*======================================================================*/
function fetch($tpl_file, $display=false){
$tpl = domxml_open_file($tpl_file);
$this->parse_tags($tpl);
$this->parse_logic_tags($tpl);
$tpl = $tpl->html_dump_mem();
$clean_tpl = $this->remove_temp_tags($tpl);
$output_tpl = $clean_tpl;
if(isset($display)){
echo $output_tpl;
return;
}
else{
return $output_tpl;
}
}
/*======================================================================*\
Function: display()
Purpose: displays the template output
\*======================================================================*/
function display($tpl_file){
$this->fetch($tpl_file, true);
}
}
?>
Here's the usage: index.php
<?php
require_once('dom_tple.class.php');
$tpl = &new dom_tple;
$tpl->assign('php', 'PHP');
//$tpl->assign('php', '<a href="http://www.php.net" target="blank">PHP</a>');
// HTML tags aren't showed properly.
$tpl->assign('dom', 'DOM');
$tpl->assign('working', 'PHP');
$tpl->display('c:/apache/htdocs/xml/template.html'); // full path to the template file.
?>
Here's the template file: template.html
<html>
<head>
<title>DOM TEMPLATE ENGINE - BETA</title>
</head>
<body>
<center><h1>Powered by <u><php /></u> and <u><dom /></u>!</h1></center>
<p align="center">
<if tag="working" op="==" value="PHP">
<b><u>PHP Working!</u></b>
<else><b>PHP doesn't working.</b></else>
</if>
</p>
</body>
</html>
After test observations:
- Note that HTML in tag values aren't showed properly.
- Note that HTML inside logic tag aren't showed properly.
Inspiration article: http://www.phppatterns.com/index.php/article/articleview/8/1/2/
Is it!
Kindest Regards,
marcoBR
Again, sorry 4 my disastrous english. :bawling: