Mahomedalid
simplemente la vida ... mahomedalid feed
revista urbana y cultural de tepic

jueves, julio 13, 2006

Un Anillo para gobernarlas a todas

... las etiquetas SGML. :)

Así nace iTagHTML, un intento (en Innox) que junto con el uso de patTemplate va eliminando el para nosotros molesto uso de etiquetas html en php.

Su código es simple, y muy probablemente poco original, pero que funciona bien (hasta el momento ;)).

Primero definimos los blue prints que utilizaremos:


define("BLUE_PRINT_DEFAULT", "<@tag_name @parameters>@html");
define("BLUE_PRINT_SELF_CLOSE", "<@tag_name @parameters />");


Estos son los "planos" (de ahí Blue Print) de las etiquetas, vamos, metaetiquetas.

Y luego la definición de la clase...

class iTagHTML
{

var $_blue_print = NULL;
var $_tag_name = NULL;
var $_parameters = NULL;
var $_html = NULL;

function iTagHTML ( $tag_name
,$parameters = array()
,$html = NULL
,$blue_print = BLUE_PRINT_DEFAULT)
{
$this->setTagName($tag_name);
$this->setHTML($html);
$this->setParameters ($parameters);
}

function getParsedTag ( $tag_name,
$parameters = array(),
$html = NULL,
$blue_print = BLUE_PRINT_DEFAULT)
{
$tag = new iTagHTML($tag_name, $parameters, $html);
$tag->setBluePrint ($blue_print);
return $tag->parseTag ();
}

function setParameters ($parameters = array ())
{
if (is_array ($parameters)) {
$this->_parameters = $parameters;
}
}

function setTagName($tag_name)
{
$this->_tag_name = $tag_name;
}

function setHTML($html) {
$this->_html = $html;
}

function setBluePrint($blue_print)
{
$this->_blue_print = $blue_print;
}

function getParameters()
{
return $this->_parameters;
}

function getBluePrint()
{
return $this->_blue_print;
}

function getTagName()
{
return $this->_tag_name;
}

function getHTML()
{
return $this->_html;
}

function parseTag ()
{
$html = "";

if ($this->getBluePrint()) {
$parameters = "";
foreach ($this->getParameters() as $name => $value) {
if (!empty($value)) {
if (!empty($name)) {
$parameters .= " {$name}='{$value}'";
} else {
$parameters .= " $value ";
}
}
}
$replace_vars = array('@tag_name' => $this->getTagName(),
'@parameters' => $parameters,
'@html' => $this->getHTML());

$html = str_replace(array_keys($replace_vars)
,$replace_vars
,$this->getBluePrint());
}
return $html;
}

/**
* Ejemplo de un shortcut y uso de iTaHTML
*/

function img ($src, $alt, $parameters)
{
return iTagHTML::getParsedTag( 'img'
,array( 'src' => $src,
'alt' => $alt,
'' => $parameters)
, NULL
, BLUE_PRINT_SELF_CLOSE);
}
}


El código se explica por si mismo. ¡Listo! :) Se admiten sugerencias :P (Todo el código y mas shortcuts aquí)

1 comentario:

Anónimo dijo...

Es la mejor abstracción HTML que he visto en toda mi vida! Excelente idea de las 'blueprints' y los shortcuts... felicidades