[php:1:7869633014]<?php
class template
{
var $parsed;
var $unparse;
var $blocks;
var $dir;
function __construct($dir)
{
$this->_setDir($dir);
}
function _setDir($dir)
{
if (is_dir($dir))
{
$this-> dir = $dir;
return true;
}
else
{
die("[TEMPLATE]Root dir no exists!");
}
}
function Assign($var, $value=null)
{
if(is_array($var))
{
reset($var);
foreach ($var as $name => $val)
{
$this -> blocks[$name] = $val;
}
}
else
{
$this -> blocks[$var] = $value;
}
}
function Display($file)
{
$this -> unparse = @file($dir . $file . '.tpl');
echo $this -> _parse();
}
function _parse()
{
$this -> parse = '';
for ($i=0; $i<= count($this->unparse); $i++)
{
$line = $this -> unparse[$i];
$found = array();
if(preg_match_all("#{(.+?)}#is", $line, $found))
{
foreach ($found[1] as $block)
{
$block_names[]='{'.$block.'}';
$block_values[] = $this -> blocks[$block];
}
$line = str_replace($block_names, $block_values, $line);
}
$this -> parsed .= $line;
}
unset($this->unparse);
return $this -> parsed;
}
function _parseSectionTag()
{
// e? co tu napisac?
}
}
?>[/php:1:7869633014]
Jak już pewnie niektórzy zauważyli jest to modyfikacja kodu ze strony Webcity.pl Dodalem pare rzeczy, usunełem niepotrzebne ale...
Mam problem ze znacznikiem {SECTION}{SECTION} uzywam go bardzo czesto więc musze napisać odpowiedni parser dla niego...
Kompletnie nie wiem jak za to się zabrać... Przykladowy tpl:
Kod
<table border="1">
{section name=config_value loop=$data}
<tr>
<td>{$data[config_value].name}</td>
<td>{$data[config_value].value}</td>
</tr>
{/section}
</table>
{section name=config_value loop=$data}
<tr>
<td>{$data[config_value].name}</td>
<td>{$data[config_value].value}</td>
</tr>
{/section}
</table>
Składnia jest taka sama jak u smartiego...
Przeglądałem kod SMARTY'iego, ale nie mogę wyciągnąć funcji za to odpowiedzialnej... strasznie zakręcone to wszystko :/
Ma ktoś pomysł jak to rozwiązać? Niech rzuci kodem, albo niech da jakas wskazówke
