Jump to content

Class template


Dat

Recommended Posts

Your honor, I object!  ;D

 

It's not really answering your question, but you could take a look at this often cited article:

 

http://www.sitepoint.com/article/beyond-template-engine

 

It might give you some new light and things to consider when thinking about a templating engine.

 

 

Link to comment
https://forums.phpfreaks.com/topic/103664-class-template/#findComment-530876
Share on other sites

	preg_match_all('/<if condition\=\"(.*)\">/U', $templatecode, $if_cond);
	foreach($if_cond[1] as $value) {
	eval("if(".$value.") {
		\$cond_result = true;
	}else{
		\$cond_result = false;
	}");
		if($cond_result == true) {
			$templatecode = preg_replace('/<if condition\=\"'.$value.'">/', '<?php if(true) { ?>', $templatecode);
		}else{
			$templatecode = preg_replace('/<if condition\=\"'.$value.'">/', '<?php if(false) { ?>', $templatecode);
		}
	}
	// change the elses
	$templatecode = preg_replace('/<else \/>/', '<?php }else{ ?>', $templatecode);

	// change the ending of the ifs
	$templatecode = preg_replace('/<\/if>/', '<?php } ?>', $templatecode);
	if($do_eval == 2) {
		return eval('?>'.$templatecode);
	}else{
		return $templatecode;
	}

 

You can figure it out yourself

Link to comment
https://forums.phpfreaks.com/topic/103664-class-template/#findComment-533811
Share on other sites

  • 2 weeks later...

	preg_match_all('/<if condition\=\"(.*)\">/U', $templatecode, $if_cond);
	foreach($if_cond[1] as $value) {
	eval("if(".$value.") {
		\$cond_result = true;
	}else{
		\$cond_result = false;
	}");
		if($cond_result == true) {
			$templatecode = preg_replace('/<if condition\=\"'.$value.'">/', '<?php if(true) { ?>', $templatecode);
		}else{
			$templatecode = preg_replace('/<if condition\=\"'.$value.'">/', '<?php if(false) { ?>', $templatecode);
		}
	}
	// change the elses
	$templatecode = preg_replace('/<else \/>/', '<?php }else{ ?>', $templatecode);

	// change the ending of the ifs
	$templatecode = preg_replace('/<\/if>/', '<?php } ?>', $templatecode);
	if($do_eval == 2) {
		return eval('?>'.$templatecode);
	}else{
		return $templatecode;
	}

 

You can figure it out yourself

 

Doesn't seem to work, but I must THANK YOU for this code.

 

Here is a link to my test file. (1) is the raw template (2) is after it replaces {CONTENT} and (3) shows the ending result after using the function you have created for me.

Link to comment
https://forums.phpfreaks.com/topic/103664-class-template/#findComment-541518
Share on other sites

Yeah I think so, I just came back to coding; you know that feeling when you look at the code that you just worked on a week ago, yeah look bizzar. I forgot what the problem I found before but I fixed it.

 

Now the last piece is actually executing the code, because it comes out as raw code.

 

I don't get this last piece...:

		if($do_eval == 2) {
		return eval('?>'.$templatecode);
	}else{
		return $templatecode;
	}

 

Link to comment
https://forums.phpfreaks.com/topic/103664-class-template/#findComment-547776
Share on other sites

Okay I nearly have it now!~

 

How can I get this: $Template->exe_template('anime', $animearray);

 

into plain text because right now I have it as $animepage = $Template->exe_template('anime', $animearray); and it's doing something that I don't want it to be doing and that is outputting on top of the page!!!

 

I know $animepage = 'works';

Link to comment
https://forums.phpfreaks.com/topic/103664-class-template/#findComment-552160
Share on other sites

How can I get this to work:

 

$animepage = $Template->exe_template('anime', $animearray);

 

Inside class.php:

	function exe_template($templatename, $arrayvar)
{
	$template = $this->buff_template($templatename);
	$template = $this->parse_variables($template, $arrayvar);
	$template = $this->parse_tags($template);
	return $template;
}

 

because when I place the above variable it prints $Template->exe_template('anime', $animearray); even though I didn't tell it too!~ and it doesn't place it into the variable!!!

Link to comment
https://forums.phpfreaks.com/topic/103664-class-template/#findComment-553143
Share on other sites

What's the code surrounding the line '$animpage =.....'?

 

Just to clarify, it seems that when your code executes this line:

 

$animepage = $Template->exe_template('anime', $animearray);

 

... The page itself displays "$Template->exe_template('anime', $animearray);", like you were echoing the text?

Link to comment
https://forums.phpfreaks.com/topic/103664-class-template/#findComment-554042
Share on other sites

Okay I got it to work finally!~! What happend was that I used eval as the return and you know it didn't come out right. So what I did was put all that off until all the replacing is done and then I used eval.

 

Now here's another issue if for some reason that I misspell condition I don't want eval to give me the normal problem message I want something like }else{ or like exit()

Link to comment
https://forums.phpfreaks.com/topic/103664-class-template/#findComment-556218
Share on other sites

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.