i've been trying to develop a template parsing system that will separate the coding from design and what not.
So i thought i was having great progress with the parser.
I was able to do a loop for content using.
<!-- START block -->
html
<!-- END block -->
but then i soon found out that i didnt like the way it was doing the looping.
I had no control over what the user sees depending on what rank or access_lvl they had.
I mean yeah i could do an if() statement cuz it'd only take one line of code.But i wanted better. Well that and i lost my last parser script with that function in it that i can never remember how to do.
So i've come here seeking help. A guy from another forum gave me this link.
anyways heres my parser.
PHP Code:
class Page{
var $page;
//--------------
// Page($template)
//--------------
function Page($template = "templates/template.tpl") {
if (is_file($template))
$this->page = implode("", file($template));
else
die("Template file $template not found.");
}
//--------------
// Parse($file)
//--------------
function parse($file) {
ob_start();
include($file);
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
//--------------
// Replace Tags
//--------------
function replace_tags($tags = array()) {
if (sizeof($tags) > 0)
foreach ($tags as $tag => $data) {
$this->page = str_replace("{" . $tag . "}", $data, $this->page);
}
}
//--------------
// Get Block
//--------------
function get_block($block){
preg_match ('#<!-- START '. $block . ' -->([^.]+)<!-- END '. $block . ' -->#',$this->page,$this->return);
$code = str_replace ('<!-- START '. $block . ' -->', "", $this->return[0]);
$code = str_replace ('<!-- END ' . $block . ' -->', "", $code);
return $code;
}
//--------------
// Remove Block
//--------------
function removeBlock($blockName){
// search a match for the expression
preg_match ('#<!-- START ' . $blockName . ' -->([^*]+)<!-- END ' . $blockName . ' -->#', $this->page, $resultCode);
// replace the match with an empty string
@$this->page = str_replace($resultCode[0], '', $this->page);
}
//*
//--------------
// Assign Block Vars
//--------------
function assign_block_vars($blockname, $tags = array())
{
foreach ($tags as $tag => $data)
{
$this->page = str_replace("{".$blockname.".". $tag . "}", $data, $this->page);
}
}
//
//--------------
// Output
//--------------
function output() {
print $this->page;
}
}
As you can see with
PHP Code:
function assign_block_vars($blockname, $data)
{
$blockCode = $this->get_block($blockname);
foreach ($data as $key => $value) {
$blockCode = str_replace ("{".$blockname.".".$value."}", $value, $blockCode);
}
$this->page = str_replace ($this->return[0], $blockCode.$this->return[0], $this->page);
}
}
I am Stumped. Because that should work.
So until then im using it as static.
i want to do kinda like how PHPBB's does it.
eg
lets say a user isnt logged in
PHP Code:
while($blah = mysql_fetch_array($result));
if($_USRDATA['user_id'] == NULL){
$template->assign_block_vars("MembersTable", array("Email" => "Logged"));
}else{
$template->assign_block_vars("MembersTable", array("Email" => $blah['email']));
}
so if user isnt logged in {MembersTable.Email} == "Logged"
ELSE {MembersTable.Email} == user@domain.tld
where you have an array of values and it'll loop through them.
IF you need more info on what i need help with jsut ask.
I've been fretting over this for a few weeks now.
And yes i know im not using a $this->get_block($blockname);
in the function mentioned.
thats becuase for sum reason it gives me a Undefined Offset 0 error. and idk why.