bueno la cosa es que me estoy haciendo un sistema de templates y le agrege un sistema para los tipicos BLOQUES (como los usados por phpBB)... pero nose porque no me esta mostrando el contenido de los bloques...
en fin, aca esta lo que llevo de codigo:
removi el enlace a la licensia de creative commons pq el foro no me dejaba postear teniendo el link...
Código PHP:
<?php
/**
*
* JTemplate
* -------------------
* - Version: v1.0 by Joablen
* - Copyright: (c) 2006 Joablen
* - License: Creative commons
*
*/
class JTemplate
{
public static $root = '';
public static $files = array();
public static $vars = array();
public static $blocks = array();
public static $includes = array();
/**
* Set template location
* @access public
*/
public function JTemplate ($root = './')
{
global $config;
if (file_exists($root . 'styles/' . $config['template_path'] . '/template'))
{
$this->root = $root . 'styles/' . $config['template_path'] . '/template';
}
else
{
die ('<br>Template path could not be found: ' . $root . 'styles/' . $config['template_path'] . '/template');
}
}
/**
* Sets the template filenames for handles.
* @access public
*/
public function set_filenames ($files_array)
{
if (!is_array($files_array))
{
return false;
}
foreach($files_array as $name => $file)
{
if(!file_exists($this->root . '/' . $file))
{
die ('<br><b>Template error:</b> The file ' . $file . ' does not exist.');
}
$this->files[$name] = $this->root . '/' . $file;
}
return true;
}
/**
* Destroy template data set
* @access public
*/
public function destroy()
{
$this->files = array();
$this->vars = array();
$this->blocks = array();
$this->includes = array();
}
/**
* Show the final result
* @access public
*/
public function display ($name, $return = false)
{
if (!$this->files[$name])
{
die ('<br><b>Template error:</b> There is no file assigned to ' . $name);
}
$this->do_includes();
print $this->process($this->files[$name]);
}
/**
* Verify each include assigned
* @access private
*/
private function do_includes ()
{
if (!empty($this->includes))
{
reset($this->includes);
foreach ($this->includes as $inc_var => $file)
{
$cont = $this->process($this->files[$file]);
$this->assign_var($inc_var, $cont);
}
}
}
/**
* Assign a file to an include var
* @access public
*/
public function assign_include ($inc_var, $file)
{
if (!$this->files[$file])
{
die("<br><b>Template error:</b> No existe archivo asignado a " . $archivo);
}
$this->includes[$inc_var] = $file;
}
/**
* Assign a value to a variable (ex: {var})
* @access public
*/
public function assign_var ($var, $val = '')
{
$this->vars[$var] = $val;
}
/**
* Assign a value to variables (ex: {var})
* @access public
*/
public function assign_vars ($vars = array())
{
foreach($vars as $var => $val)
{
$this->vars[$var] = $val;
}
}
/**
* Display a block and assign it's vars
* @access public
*/
public function assign_block_vars ($block, $vars)
{
if (strpos($block, '.'))
{
$blocks = explode('.', $block);
$blockcount = sizeof($blocks) - 1;
$str = '$this->blocks';
for ($i = 0; $i < $blockcount; $i++)
{
$str .= '[\'' . $blocks[$i] . '\']';
eval('$num_interactions = sizeof(' . $str . ') - 1;');
$str .= '[' . $num_interactions . ']';
}
$str .= '[\'' . $blocks[$blockcount] . '\'][] = $vars;';
eval($str);
}
else
{
$this->blocks[$block][] = $vars;
}
}
/**
* Parse blocks
* @access public
*/
public function block($text, $object, $block, $orig_block = '', $sub = false)
{
if (empty($block))
{
return $text;
}
$block_text = '';
if (preg_match("/<!-- BEGIN " . $block . " -->(.*?)<!-- END " . $block . " -->/sU", $text, $matches))
{
$block_text = $matches[1];
}
$orig_block = ($sub == true) ? $orig_block . "." : "";
$final_blocks = array();
$count = sizeof($object["$block"]) - 1;
for ($i = 0; $i <= $count; $i++)
{
$orig_block = $repl_block = $subblock = array();
foreach ($object["$block"][$i] as $var => $val)
{
if (is_array($val) && !in_array($var, $subblock) && !empty($var))
{
$subblock[] = $var;
}
else
{
$orig_block[] = "/{" . $orig_block . $block . "." . $var . "}/";
$repl_block[] = preg_replace(array('/{/', '/}/'), array('', ''), $val);
}
}
$proc_text = $block_text;
$count2 = sizeof($subblock) - 1;
for ($m = 0; $m <= $count2; $m++)
{
$proc_text = $this->block($proc_text, $object["$block"][$i], $subblock[$m], $block, true);
}
$final_blocks["$block"] .= preg_replace($orig_block, $repl_block, ' ' . $proc_text . ' ');
}
$text = ($sub == true) ? preg_replace("@<!--\s+BEGIN\s+" . $block . "\s+-->(.*?)<!--\s+END\s+" . $block . "\s+-->@sU", $final_blocks["$block"], $text) : $final_blocks["$block"];
return $text;
}
/**
* Process the template
* @access private
*/
private function process ($file)
{
$cont = implode("", @file($file));
if (empty($cont))
{
die ('<br><b>Template error:</b> The file is empty');
}
// Replace includes with the included content
preg_match_all('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\./]+?) -->#', $cont, $matches);
foreach ($matches[1] as $match => $file)
{
$cont = str_replace("<!-- INCLUDE " . $file . " -->", @file_get_contents($this->root . '/' . $file), $cont);
}
// Replace vars with their value
if(!empty($this->vars))
{
reset($this->vars);
foreach ($this->vars as $var => $val)
{
$cont = str_replace("{" . $var . "}", preg_replace(array('/{/', '/}/'), array('', ''), $val), $cont);
}
}
// Do blocks
$final_blocks = array();
if(!empty($this->blocks))
{
reset($this->blocks);
foreach ($this->blocks as $block)
{
$final_blocks["$block"] = $this->block($cont, $this->blocks, $block, '', false);
$cont = preg_replace("@<!--\s+BEGIN\s+" . $block . "\s+-->(.*?)<!--\s+END\s+" . $block . "\s+-->@sm", $final_blocks["$block"], $cont);
}
}
// Clear unused stuff
$cont = preg_replace('@<!--\s+BEGIN\s+([0-9A-Za-z_-]+)\s+-->(.*)<!--\s+END\s+\1\s+-->@sU', '', $cont);
$cont = preg_replace('/{(.*?)}/', '', $cont);
return $cont;
}
}
?>