DIR Return Create A Forum - Home
---------------------------------------------------------
The \"YourNeighbour\" project
HTML https://yourneighbour.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: Dev & design talk
*****************************************************
#Post#: 380--------------------------------------------------
Style parsing script
By: anonymous Date: July 13, 2012, 2:50 am
---------------------------------------------------------
I'm creating universal style parsing script, style format like
from phpBB2:
[code]<h1>{TEST}</h1>
<!-- BEGIN TEST -->
<h1>{test.replaceit}</h1>
<!-- END TEST -->
<!-- BEGIN TEST2 -->
<h1>{test2.replaceit}</h1>
<!-- END TEST2 -->
<!-- IF TEST3 -->
<h1>aaaaaa</h1>
<!-- ENDIF TEST3 -->[/code]
It now looks like:
[code]
<?php
class Styler {
public $style = "light";
public $code = "<h1>Template error</h1>";
public $variables = array("TEST" => "agsdgasdf");
public $blocks = array("TEST" => array(1 => array("replaceit"
=> "hahaha"), 2 => array("replaceit" => "hahaha2")), "TEST2" =>
array(1 => array("replaceit" => "hah1aha"), 2 =>
array("replaceit" => "ha3haha2")));
//$name - e.g. test.tpl; testdir/test.tpl
function loadFile($filename)
{
$url = "./templates/".$this->style."/html/".$filename;
if ( ( $string = @file_get_contents( $url ) ) == false )
return -1;
$this->code = $string;
return;
}
function addBlock($name, $value)
{
$blocks[$name][] = $value;
}
function parseTemplate()
{
foreach ($this->variables as $key => $value)
{
$this->code = str_replace("{".$key."}", $value, $this->code);
}
$this->code = parseBlocks($this->code);
echo '
'.$this->code;
}
protected function parseBlocks($code, $block = $this->block)
{
preg_match_all("@<!-- BEGIN (.*?) -->(.*?)<!-- END \\1
-->@siu", $this->code, $matches);
for ($i = 0; $i < count($matches[1]); $i++)
{
$repl = $matches[0][$i];
$name = $matches[1][$i];
$code = $matches[2][$i];
$compiled = "";
if (isset($block[$name]))
{
//array ("NAME" => array(1 => array("NAME" =>
//parseblocks here or..
foreach ($block[$name] as $occ)
{
$temp = $code; //here
foreach ($occ as $name => $value)
{
if (is_array($value)
{
//WTF??? Arrayception...
} else {
$temp = str_replace("{".$name."}", $value, $temp);
}
}
$compiled .= $temp;
}
}
$code = str_replace($repl, $compiled, $code);
return $code;
}
}
}
?>
[/code]
Almost everything works.
<!-- IF (.*?) doesn't work and <!-- BEGIN XXX --><!-- BEGIN XX2
--><!-- END XX2 --><!-- END XXX-->
*****************************************************