
› Scripts › Template Parser
|
|
PHP & Snippers › Template Parser
|
|
Geplaatst op: 08-08-2005 16:57 / Auteur: Gert-Jan / 3995 keer bekeken
|
Info
Ooit gemaakt voor een wedstrijd op WF, nu met een kleine modificatie hier 
Functies
SetTemplate: Het laden van een template, dit kan via een database/file array.
SetBlock: Het oproepen van een bepaalde blok.
GotoBlock: Het terug zetten naar een bepaald blok, dit gebruiken na elk blok wanneer je nieuwe (Niet geneste blok) wilt oproepen.
SetVar: Het oproepen van een variabele.
ReturnContent: Het ophalen van de geparsde template
PrintContent: Printen van geparsde template.
|
<?PHP
# Author : Gert-Jan (gert-jan at gert-jan dot ath dot cx)
class noName_1
{
## Private vars:
var $cblockid = -1; // Counts number of blocks.
var $parsed = null;
var $parentid = null; // Contains the ID of the current parent.
var $content = null; // Template content.
var $bsequence = array (); // Sequence on which the blocks are called.
var $blockdata = array (); // Containst data from the blocks.
## Template loader.
function setTemplate ($template, $byvar = null)
{
$content = '';
if(!is_array ($template ))
{
if(!$byvar)
$content = fread(fopen($template, "r"), filesize($template)) or die("Template ".$template." does not exist");
else
$content = $template;
}
else
{
foreach($template as $template)
{
if(!$byvar)
$content .= fread(fopen($template, "r"), filesize($template)) or die("Template ".$template." does not exist");
else
$content .= $template;
}
}
if(!$this->content)
$this->setblock('_ROOT', TRUE);
$this->content .= $content;
}
## Block summon function.
function setBlock ($blockname, $parent = NULL)
{
$this->cblockid = count($this->bsequence);
$this->bsequence [$this->cblockid] = $blockname;
$this->blockdata [$this->cblockid]['prnt'] = $this->parentid;
$this->parentid = ($parent) ? $this->cblockid : $this->parentid;
}
## Goto block function, block is set as parent.
function gotoBlock ($blockname)
{
$array = array_reverse(array_keys($this->bsequence, $blockname));
$this->cblockid = $array[0];
$this->parentid = $array[0];
}
## SetVar
function setVar ($var, $rep = NULL)
{
if(is_array($var))
{
foreach($var as $var => $rep)
$this->blockdata[$this->cblockid['vars']][] = array(0 => $var, 1 => $rep);
}
else
$this->blockdata[$this->cblockid]['vars'][] = array(0 => $var, 1 => $rep);
}
## Return content
function returnContent ()
{
if(!$this->parsed)
$this->_parse();
return $this->content;
}
## Print content
function printContent ()
{
if(!$this->parsed)
$this->_parse();
print $this->content;
}
## Parse function
function _parse ()
{
$array = $this->_extract( $this->content);
$blocks = $array[0];
$parts = $array[1];
$content = $array[2];
$content = preg_replace("!\[block:(.*?)]!", "[block:0.\\1]", $content);
for ($i = 1; $i < count( $this->bsequence); $i++)
{
$name = $this->bsequence[$i];
$cont = $blocks[$name];
$cont = preg_replace("#\[block:(.+)]#is", "[block:".$i.".\\1]", $cont);
$content = str_replace('[block:'.$this->blockdata [$i]['prnt'].'.'.$name.']', $cont.'[block:'.$this->blockdata [$i]['prnt'].'.'.$name.']', $content);
if(isset($this->blockdata[$i]['vars']))
{
$c = count($this->blockdata[$i][ 'vars' ]);
for( $j = 0; $j < $c; $j++ )
{
$content = str_replace('{'.$this->blockdata[$i]['vars'][$j][0].'}', $this->blockdata[$i]['vars'][$j][1], $content);
}
}
}
$this->parsed = true;
$this->content = preg_replace("!\[(block|part):(.*?)]!", '', $content);
}
## Template extract function.
function _extract ($cont)
{
$blocks = array(); // Contains all blockdata
$parts = array(); // Contains all partsdata
$apos = 0; // Where was the last start + 1.
## Looping the content.
while(is_int($start = strpos( $cont, '[', $apos)))
{
$apos = $start + 1; // Set apos
$pos = strpos($cont, '=', $start); // Get position of = tag
$spos = $pos + 1; // set of spos
$epos = strpos( $cont, ']', $pos); // get end delim
$name = substr( $cont, $spos, $epos - $spos); // get name of tag
## This explains itself.
$stag = '[block='.$name.']';
$etag = '[/block='.$name.']';
$epos = strpos( $cont, $etag, $epos); // Postion of end tag
## Error handling
if(!$epos)
die('No end found for block: '.$name);
## Count length of tags
$stag = $start + strlen ($stag);
$etag = $epos + strlen ($etag);
## Cutting content.
$ccon = substr ($cont, $stag, $epos - $stag);
$par1 = substr ($cont, 0, $start);
$par2 = substr ($cont, $etag, strlen($cont));
## Rerun this function with diffrent content
$ccon = $this->_extract($ccon);
## Merge arrays
$blocks = array_merge_recursive($blocks, $ccon[0]);
$parts = array_merge_recursive($parts, $ccon[1]);
## Explains itself
$blocks [$name] = $ccon[2];
$cont = $par1.'[block:'.$name.']'.$par2;
}
## return an array.
return array($blocks, $parts, $cont);
}
}
?>
|
|
|

› Waardering
|
|
Op dit moment is er 5 keer gestemd op dit onderwerp. De gemiddelde score is 3.6.
Je kunt niet stemmen omdat je al gestemd hebt of omdat je niet bent ingelogd.
|
|
|   Leden | |   Actieve topics | |   Linkpartners | |   Overige | | |