Jump to content

Class template


Dat

Recommended Posts

Where can I find a tutorial on how to make a if statement in a template.

 

Example:

 

<if condition="$var">
<!-- Do something -->
</if>

 

So when phrased it will:

 

if($var)
{
Do it
}

Link to comment
Share on other sites

That's the thing, I'm creating my own oop class template system. I have this website, and I want to learn so I like to create everything by me. except for the forum but that's not related to this topic.

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

You're gonna have to give me more code to go on -- that doesn't tell me anything.  What is that function, and what is being put at the top of the page?  And do you know $animepage contains what you want it to (ie, the variable is working) or literally $animepage == 'works'?

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

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.