Jump to content

Template System <if> statements


rm249

Recommended Posts

Hello, I am pretty good in PHP although I am having a bit of trouble with this regular expression. I am working on a template system function to parse the inputed template file as a variable and find and parse any <if> statements. The way it works is that it detects if the variable is defined or not, if it is show the results, else dont. (Its basic right now but I intend to expand more upon it once I get the basics working correctly).

 

The way I have it setup works great when there aren't any <if> statements within another if, but when there are everything goes all foobar.

 

Any tips as to how to get this to work properly with <if> statements within if statements are greatly appreciated.

 

An example of how the IF is in the template file is below:

<if="cu_user_name">
Welcome [cu_user_name]<br />
<if="cu_admin">
	Click to go to admin<br />
</if>
</if>

 

PHP function that parses the <if> statements:

// Parse if statememnts
function ifParse($handle,$vars) {
preg_match_all('#<if=\"(.*)\">(.*)</if>#Us',$handle,$results);
if (count($results)>0) {
	while(list($key,$val) = each($results[1])) {
		$data = $results[2][$key];
		$pattern = '#<if="'.$val.'">(.*)</if>#Us';
		if(array_key_exists($val,$this->vars)) {
			$handle = preg_replace($pattern,"$1",$handle);
		}
		else {
			$handle = preg_replace($pattern,"",$handle);
		}			
	}
	return $handle;
}
else {
	return false;
}
}

Link to comment
https://forums.phpfreaks.com/topic/74934-template-system-statements/
Share on other sites

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.