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
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.