[eluser]tazoony[/eluser]
Hi !
In our applications, it's not always easy to comply with MVC rules.
I thought it could be great to describe a page with a library, OOP.
I wrote a small and very very very simple library named "HTMLTag", to comply with MVC rules in my CRUD automated library.
So, here an using example :
Code:
$this->load->library('HTMLTag');
$page = new HTMLTag('body',
array('class'=>'lalala'),
array(
new HTMLTag('div', array('class'=>'totototot'),
new HTMLTag('form', array('method'=>'post'),
new HTMLTag('input',array('type'=>'button'))
)
),
new HTMLTag('p', array('style'=>"font-weight:bold;"), "Hi there !!")
)
);
echo $page;
Output (with automatic indentation) :
Code:
<body class="lalala">
<div class="totototot">
<form method="post">
<input type="button" />
</form>
</div>
<p >
Hi there !!
</p>
</body>
It could be helpful to manage interfaces, buttons, etc.
We can extend this library to specific usage (automated form interactions for example, or ajax forms, etc.).
Here the class code :
Code:
<?php
/**
* HTMLTag Library for PHP
* Helpful to manage HTML interfaces
* @author Joffrey Letuve <jletuve[at]gmail[dot]com>
*/
define ( 'HTMLTAG_TYPE_SIMPLE' , 1 );
define ( 'HTMLTAG_TYPE_DOUBLE' , 2 );
class HTMLTag {
private $_tags_types = array(
// Classics
'html' => HTMLTAG_TYPE_DOUBLE,
'head' => HTMLTAG_TYPE_DOUBLE,
'body' => HTMLTAG_TYPE_DOUBLE,
'div' => HTMLTAG_TYPE_DOUBLE,
'a' => HTMLTAG_TYPE_DOUBLE,
'p' => HTMLTAG_TYPE_DOUBLE,
'br' => HTMLTAG_TYPE_SIMPLE,
'img' => HTMLTAG_TYPE_SIMPLE,
'span' => HTMLTAG_TYPE_DOUBLE,
// Forms
'form' => HTMLTAG_TYPE_DOUBLE,
'fieldset' => HTMLTAG_TYPE_DOUBLE,
'legend' => HTMLTAG_TYPE_DOUBLE,
'label' => HTMLTAG_TYPE_DOUBLE,
'input' => HTMLTAG_TYPE_SIMPLE,
'button' => HTMLTAG_TYPE_DOUBLE,
'textarea' => HTMLTAG_TYPE_DOUBLE,
'checkbox' => HTMLTAG_TYPE_SIMPLE,
'radio' => HTMLTAG_TYPE_SIMPLE,
'select' => HTMLTAG_TYPE_DOUBLE,
'option' => HTMLTAG_TYPE_DOUBLE,
'optgroup' => HTMLTAG_TYPE_DOUBLE,
// Tables
'table' => HTMLTAG_TYPE_DOUBLE,
'caption' => HTMLTAG_TYPE_DOUBLE,
'thead' => HTMLTAG_TYPE_DOUBLE,
'tbody' => HTMLTAG_TYPE_DOUBLE,
'tfoot' => HTMLTAG_TYPE_DOUBLE,
'th' => HTMLTAG_TYPE_DOUBLE,
'tr' => HTMLTAG_TYPE_DOUBLE,
'td' => HTMLTAG_TYPE_DOUBLE,
// Heading
'h1' => HTMLTAG_TYPE_DOUBLE,
'h2' => HTMLTAG_TYPE_DOUBLE,
'h3' => HTMLTAG_TYPE_DOUBLE,
'h4' => HTMLTAG_TYPE_DOUBLE,
'h5' => HTMLTAG_TYPE_DOUBLE,
'h6' => HTMLTAG_TYPE_DOUBLE,
// Lists
'ul' => HTMLTAG_TYPE_DOUBLE,
'ol' => HTMLTAG_TYPE_DOUBLE,
'li' => HTMLTAG_TYPE_DOUBLE,
'dl' => HTMLTAG_TYPE_DOUBLE,
'dt' => HTMLTAG_TYPE_DOUBLE,
'dd' => HTMLTAG_TYPE_DOUBLE,
);
var $name = 'div'; // Tag name :
private $type = HTMLTAG_TYPE_SIMPLE ; // Type : Simple or Double (<br /> or <a></a>)
private $attributes = array(); // Inline attributes
private $inner_htmltags = array(); // Inner elements for DOUBLE tags
public function __construct( $name='' , $attributes=array() , $inner_elements=array() )
{
if ( !empty($name) )
{
$this->name = strtolower($name) ;
$this->attributes = $attributes ;
if ( !is_array($inner_elements) )
$inner_elements = array($inner_elements);
$this->inner_htmltags = $inner_elements ;
if ( array_key_exists($name, $this->_tags_types) )
$this->type = $this->_tags_types[$name];
}
}
function __toString()
{
return $this->get_html();
}
function get_html($indentation = 0)
{
$str_indent = $this->indentation($indentation);
$html = "";
$html .= $str_indent;
$html .= '<'.$this->name.' '.$this->get_inline_attributes();
if ( $this->type == HTMLTAG_TYPE_SIMPLE )
{
$html .= ' />';
}
else
{
$html .= ">";
foreach($this->inner_htmltags as $inner_html)
{
if ( is_object($inner_html) ) {
$html .= "\r\n";
$html .= $inner_html->get_html($indentation+1);
} else {
$html .= "\r\n".$str_indent.$inner_html;
}
}
$html .= "\r\n";
$html .= $str_indent.'</'.$this->name.'>';
}
return $html;
}
function indentation($indent_count=1) {
$str = '';
for ( $i=1 ; $i<=$indent_count ; $i++ )
{
$str .= "\t";
}
return $str;
}
function get_inline_attributes()
{
$array_str = array();
foreach($this->attributes as $attr_name => $attr_value) {
$array_str[] = strtolower($attr_name).'="'.strtolower($attr_value).'"';
}
return implode(' ',$array_str);
}
}
Joffrey