tom92 Posted November 25, 2008 Share Posted November 25, 2008 Hey, Ive been working on my own template system that parsers a static html file so it's easier to update. But say if I wanted to create a function that fetches data from mysql or something and replace this with... $tags_array = array('header'=>'<h1>Header</h1>','center'=>MyFunction(),'footer'=>'<h2>Footer</h2>'); The output from that function appears at the very top of the page for some reason. Any help please? Thanks. Heres the code i'm using... <?php function parse_template($template,$tags_array){ $template = file_get_contents($template); foreach($tags_array as $current_tag => $new_tag){ $template = str_replace("{".$current_tag."}",$new_tag,$template); } return $template; } $tags_array = array('header'=>'<h1>Header</h1>','footer'=>'<h2>Footer</h2>'); echo parse_template('example_template.html',$tags_array); ?> Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/ Share on other sites More sharing options...
trq Posted November 25, 2008 Share Posted November 25, 2008 What does MyFunction() look like? Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-698950 Share on other sites More sharing options...
tom92 Posted November 25, 2008 Author Share Posted November 25, 2008 Something like... while($row = mysql_fetch_array($query)){ echo $row['example']; } Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-698988 Share on other sites More sharing options...
trq Posted November 25, 2008 Share Posted November 25, 2008 Your function needs to return its data, not echo it. Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-698994 Share on other sites More sharing options...
tom92 Posted November 26, 2008 Author Share Posted November 26, 2008 Thanks it works! Say if i need to return more than 1 lines of data it doesnt seem to work is there any way around this? Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-699654 Share on other sites More sharing options...
trq Posted November 26, 2008 Share Posted November 26, 2008 You would need to return an array. Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-699667 Share on other sites More sharing options...
tom92 Posted November 26, 2008 Author Share Posted November 26, 2008 Okay. I've tried this just to test it out... <?php $array = array('one','two','three','four','five'); foreach($array as $key => $value){ return $key.$value; } ?> But it only seems to output the element of the array? Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-699745 Share on other sites More sharing options...
Eric_Ryk Posted November 27, 2008 Share Posted November 27, 2008 Okay. I've tried this just to test it out... <?php $array = array('one','two','three','four','five'); foreach($array as $key => $value){ return $key.$value; } ?> But it only seems to output the element of the array? You have to concatenate all of the values into one string. Then outside of the loop return the finished string. Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-700249 Share on other sites More sharing options...
tom92 Posted November 27, 2008 Author Share Posted November 27, 2008 I'm not trying to be a pain but could u show me an example of how to do this please? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-700278 Share on other sites More sharing options...
trq Posted November 27, 2008 Share Posted November 27, 2008 To assist with a better example, can we see the entire MyFunction() function? Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-700313 Share on other sites More sharing options...
tom92 Posted November 27, 2008 Author Share Posted November 27, 2008 Sure... Say i wanted to display some results from mysql and show how many rows there are... <?php function MyFunction(){ $query = mysql_query("SELECT * FROM example"); while($row = mysql_fetch_array($query)){ return $row['id'].' - '.$row['name']; } return 'There are '.mysql_num_rows($query).' rows in the database.'; } ?> Where would i go from there? Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-700391 Share on other sites More sharing options...
trq Posted November 27, 2008 Share Posted November 27, 2008 I think you need to take another look at what return actually does. This isn't pretty, but its your design. <?php function MyFunction() { if ($result = mysql_query("SELECT id, name FROM example")) { if (mysql_num_rows($result)) { $return = array(); $return['num_rows'] = mysql_num_rows($result); while ($row = mysql_fetch_array($result)) { $return['result'][] = array($row['id'], $row['name']); } return $return; } } return false; } ?> And now to call it... <?php if ($array = MyFunction()) { echo "There are {$array['num_rows']} rows in the database."; foreach ($array['result'] as $row) { echo $row['id'] . ' - ' . $row['name']; } } ?> Ive changed your variable names around a little to make them more descriptive. Also, put some error handling in place. Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-700402 Share on other sites More sharing options...
tom92 Posted November 27, 2008 Author Share Posted November 27, 2008 Thanks! works a treat. Is this really the best way to be templating in php is there any other ways you would recommend? Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-700403 Share on other sites More sharing options...
trq Posted November 27, 2008 Share Posted November 27, 2008 I wouldn't bother with any templating, it just adds overhead you don't need. Its easy enough to design your code in such a way that there is only minimal amounts of php mixed in with html. Just my 2 cents though. Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-700407 Share on other sites More sharing options...
Eric_Ryk Posted November 28, 2008 Share Posted November 28, 2008 I wouldn't bother with any templating, it just adds overhead you don't need. Its easy enough to design your code in such a way that there is only minimal amounts of php mixed in with html. Just my 2 cents though. I'm in agreement with thorpe. Though to add, it's usually like having "template" files that use PHP within them, though only minimally as thorpe stated. ie: <p>Some static stuff... <b>Hi <?php echo $name; ?></b></p> Typically within these files instead of using if and other control statements with brackets i use the alternate structures. Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-700756 Share on other sites More sharing options...
tom92 Posted November 28, 2008 Author Share Posted November 28, 2008 Yeah, I've decided to completley scrap using a templating system after all that lol. I'm simply just going to use a dreamweaver template with some embeded php lol. Like thorpe said I guess templating is just another overhead. Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-701228 Share on other sites More sharing options...
Liquid Fire Posted November 28, 2008 Share Posted November 28, 2008 I wouldn't bother with any templating, it just adds overhead you don't need. Its easy enough to design your code in such a way that there is only minimal amounts of php mixed in with html. Just my 2 cents though. I don't agree with that 100%. As a programmer I don't see a hugh difference but i know a few designer that perfer a template system then php. Also if the template system does compiling and caching right, the over head is only on the first call and then it is barely noticeable like Smart/Dwoo(not something like XTemplates which does not compile of cache). Quote Link to comment https://forums.phpfreaks.com/topic/134233-template-help/#findComment-701341 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.