Destramic Posted November 9, 2010 Share Posted November 9, 2010 im trying to make a simple template engine but am having a problem replacing the string in the content....if anyone could give me any help or pointers please <?php class Template { public function display($filename) { echo "hello"; } public function assign($variable, $value) { if (!is_array($value)) { ob_start(); $contents = ob_get_contents(); $contents = str_replace($variable, $value, $contents); ob_end_clean(); return $contents; } } } $template = new Template(); $template->display(); $template->assign("hello", "test"); ?> code above will only display hello and not test as it should Quote Link to comment https://forums.phpfreaks.com/topic/218199-oop-simple-template-class/ Share on other sites More sharing options...
monkeytooth Posted November 9, 2010 Share Posted November 9, 2010 Ok? Well what is the expected output vs the actual output. Cause right now at least It seems to be working as coded. Quote Link to comment https://forums.phpfreaks.com/topic/218199-oop-simple-template-class/#findComment-1132235 Share on other sites More sharing options...
Destramic Posted November 9, 2010 Author Share Posted November 9, 2010 im trying to simply replace hello with test but its not working...just outputting hello Quote Link to comment https://forums.phpfreaks.com/topic/218199-oop-simple-template-class/#findComment-1132243 Share on other sites More sharing options...
Adam Posted November 9, 2010 Share Posted November 9, 2010 You're just echoing out a static value: public function display($filename) { echo "hello"; } Plus it doesn't make logical sense to try and display() the template, before you've assign()ed your vars..? $template->display(); $template->assign("hello", "test"); Quote Link to comment https://forums.phpfreaks.com/topic/218199-oop-simple-template-class/#findComment-1132245 Share on other sites More sharing options...
Destramic Posted November 9, 2010 Author Share Posted November 9, 2010 i understand that but i just want to be able to replace that before i improve the script....but i think it would make sense to replace after you've displayed the file? if someone could help me to replace the sting? Quote Link to comment https://forums.phpfreaks.com/topic/218199-oop-simple-template-class/#findComment-1132247 Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2010 Share Posted November 9, 2010 There's no code in your code to do anything other than echo 'hello'. In order for you to write a class to implement even a simple template, you must first define what you want each of your class methods to do and what class variables you will need. You have just thrown some random code into a class structure. Quote Link to comment https://forums.phpfreaks.com/topic/218199-oop-simple-template-class/#findComment-1132248 Share on other sites More sharing options...
Destramic Posted November 9, 2010 Author Share Posted November 9, 2010 PFMaBiSmAd im trying to replace hello with test can you explain what is wrong please? Quote Link to comment https://forums.phpfreaks.com/topic/218199-oop-simple-template-class/#findComment-1132250 Share on other sites More sharing options...
xangelo Posted November 9, 2010 Share Posted November 9, 2010 It doesn't make sense to output code before you make all your changes to it, because now you're wasting cycles and time doing string manipulations. A much easier method would be to have an associative array of things that you'd like to change, and their values. assign() will simply set the value of the array key and display() will simply use each value from the array. Quote Link to comment https://forums.phpfreaks.com/topic/218199-oop-simple-template-class/#findComment-1132251 Share on other sites More sharing options...
MatthewJ Posted November 9, 2010 Share Posted November 9, 2010 $template->display("test"); public function display($filename) { echo $filename; } Aside from the other things people suggested, the above changes would accomplish what you want... Quote Link to comment https://forums.phpfreaks.com/topic/218199-oop-simple-template-class/#findComment-1132255 Share on other sites More sharing options...
Destramic Posted November 9, 2010 Author Share Posted November 9, 2010 yeah ok that makes sence but how do i get the content and replace the strings please? Quote Link to comment https://forums.phpfreaks.com/topic/218199-oop-simple-template-class/#findComment-1132258 Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2010 Share Posted November 9, 2010 As someone already suggested, you need to define everything before you can write any code to do it. For example, where is your template stored? You class constructor should accept, as a parameter, the name of the template to use and then read it from wherever it is stored and place it into a class variable. Once you have the raw template available in an instance of your class, you can then think about replace values in it. Once you have replaced all the values in it, you can think about displaying it. Quote Link to comment https://forums.phpfreaks.com/topic/218199-oop-simple-template-class/#findComment-1132266 Share on other sites More sharing options...
monkeytooth Posted November 9, 2010 Share Posted November 9, 2010 you set strings in the template that would serve as markers to be replaced by strings that are dynamic.. Static Template: $template = 'My name is {username}, i like to whatever...'; Replace items in Static Template.. str_replace('{username}', $filename, $template); revision.. something along the lines of.. public function display($filename) { $template = 'My name is {username}, i like to whatever...'; echo str_replace('{username}', $filename, $template); } super basic concept of how the idea is to work, but hopefully it gives you the idea.. now knowing my luck as I didnt test that im sure theres a bug in it, but the idea is the same regardless. Quote Link to comment https://forums.phpfreaks.com/topic/218199-oop-simple-template-class/#findComment-1132267 Share on other sites More sharing options...
Adam Posted November 9, 2010 Share Posted November 9, 2010 If you're going to go down the road of parsing templates like that, then you're also going to need to think about how you'll interpret control structures; like if conditions, loops, includes, etc. If you plan on using normal PHP for those, then what's the point in the overhead of parsing templates just to replace variables? You'd may as well just use PHP-based templates in that case. Quote Link to comment https://forums.phpfreaks.com/topic/218199-oop-simple-template-class/#findComment-1132276 Share on other sites More sharing options...
erayfield Posted November 9, 2010 Share Posted November 9, 2010 I commented out your first display call, that is what was showing the first echo, then, since I didn't know where you were getting for ob_get_content(), just threw in a string to be returned - try this: <?php class Template { /************************************************* * echos out the passed in data * @param string $valueToDisplay what should be the screen output * @return void *************************************************/ public function display($valueToDisplay) { echo $valueToDisplay; } /************************************************* * replaces the variable with the value * * @param string $variable - what should be replaced * @param string $value - value to insert * @return string containing value rather than variable *************************************************/ public function assign($variable, $value) { if (!is_array($value)) { ob_start(); $contents = $this->get_page(); $contents = str_replace($variable, $value, $contents); ob_end_clean(); return $contents; } } /************************************************* * gets web page, with 'placeHolder' set * * @return string of html page **************************************************/ protected function get_page() { return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><title> placeHolder </title></head><body>hi, this is your web page, <br /><h2>placeHolder</h2></body></html>'; } } $placeHolder = "placeHolder"; $replacePlaceHolderWithThis = "replaced place holder with this string "; $template = new Template(); //$template->display($placeHolder); $page = $template->assign($placeHolder, $replacePlaceHolderWithThis); $template->display($page); ?> Quote Link to comment https://forums.phpfreaks.com/topic/218199-oop-simple-template-class/#findComment-1132374 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.