Dat Posted May 1, 2008 Share Posted May 1, 2008 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 } Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted May 1, 2008 Share Posted May 1, 2008 It depends on the template engine you are working with, please check the documentation of your template system. Quote Link to comment Share on other sites More sharing options...
Dat Posted May 1, 2008 Author Share Posted May 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
Aeglos Posted May 1, 2008 Share Posted May 1, 2008 Your honor, I object! 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. Quote Link to comment Share on other sites More sharing options...
Dat Posted May 1, 2008 Author Share Posted May 1, 2008 How about something that I can do similar to vbulletin's template system. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted May 4, 2008 Share Posted May 4, 2008 Have a look at Smarty. Smarty is my favourite template engine, hasn't let me down so far, and has support for the features such as if, foreach etc. etc. Quote Link to comment Share on other sites More sharing options...
blu3t00th Posted May 5, 2008 Share Posted May 5, 2008 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 Quote Link to comment Share on other sites More sharing options...
Dat Posted May 15, 2008 Author Share Posted May 15, 2008 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. Quote Link to comment Share on other sites More sharing options...
nloding Posted May 16, 2008 Share Posted May 16, 2008 That code fragment is missing a " and, I believe, a ) in the eval statement. Quote Link to comment Share on other sites More sharing options...
blu3t00th Posted May 22, 2008 Share Posted May 22, 2008 eval("if(".$value.")") { That should fix it, it was from my un finished/tested template system. Its a bit slopy but it does the job :/ Did you get it working? Quote Link to comment Share on other sites More sharing options...
Dat Posted May 23, 2008 Author Share Posted May 23, 2008 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; } Quote Link to comment Share on other sites More sharing options...
Dat Posted May 23, 2008 Author Share Posted May 23, 2008 Actually my real question is why do I have to include '?>' in the eval function: return eval('?>'.$templatecode); Quote Link to comment Share on other sites More sharing options...
blu3t00th Posted May 23, 2008 Share Posted May 23, 2008 I had that for something else, all my code is all mixed together, just pulled it out so you would get the idea n how to work it Try getting rid of all 5 of those lines and just use return $templatecode; Quote Link to comment Share on other sites More sharing options...
Dat Posted May 26, 2008 Author Share Posted May 26, 2008 Why does it have the '?>' in it because, right now I want to embed php in my template and I can't do that unless I have that... Quote Link to comment Share on other sites More sharing options...
Dat Posted May 28, 2008 Author Share Posted May 28, 2008 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'; Quote Link to comment Share on other sites More sharing options...
nloding Posted May 29, 2008 Share Posted May 29, 2008 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'? Quote Link to comment Share on other sites More sharing options...
Dat Posted May 30, 2008 Author Share Posted May 30, 2008 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!!! Quote Link to comment Share on other sites More sharing options...
nloding Posted May 31, 2008 Share Posted May 31, 2008 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? Quote Link to comment Share on other sites More sharing options...
Dat Posted June 3, 2008 Author Share Posted June 3, 2008 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() Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.