marcusfaye87 Posted May 18, 2008 Share Posted May 18, 2008 Hello again, I'm the kind of programmer who likes to have their source code sepperated from the layouting in PHP. I don't know what would be the best way to do this. I could use templates, but I have no experiance in that area. I already use a lot of functions and think I might switch to OOPHP. But I don't have any Idea of how to start at that. I have not gotten around implementing code in my Layout page. For example, when I have my comments page of the newsposts, There is a box that submits information to the database. And it sort of has to always have a bit of PHP code. Even if it's a simple as formatting how the comments are shown, Or processing the POST data. What would be the best way to go at it, And if the answer has to be templates, any good tutorials you'd recommend? Thanks in advance, Marcus Quote Link to comment Share on other sites More sharing options...
chronister Posted May 18, 2008 Share Posted May 18, 2008 Well, for templating here is what I use. I am not even sure if this is what you referring to... but here goes. header.php -- This page controls the layout for all other pages. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title><?php echo $page_title; ?></title> </head> <body> <div id="header"></div> <div id="left_nav"></div> <div id="main_body"> <!-- All pages load here --> <?php function footer(){ ?> </div> <div id="footer"></div> </body> </html> <?php } ?> I typically leave header.php in the root of the dir and then call it on my pages like this. anotherpage.php <?php $page_title = 'Page Title'; include($_SERVER['DOCUMENT_ROOT'].'/header.php'); ?> Page content goes here <?php footer(); ?> Hope this helps. Nate Quote Link to comment Share on other sites More sharing options...
marcusfaye87 Posted May 18, 2008 Author Share Posted May 18, 2008 No I already use that. I have a container file that uses a switch to show each page itself, but I mean, to format a news post I have found no other way then to echo alle the HTML with the variables. but by templating I mean, creating all of the HTML separately and change words to php code For example: if you want a title somewhere you put {::TITLE::} in the HTML file and make PHP load in that file and replace your key words and print out that data. Quote Link to comment Share on other sites More sharing options...
marcusfaye87 Posted May 21, 2008 Author Share Posted May 21, 2008 Page 9, time for a bump Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 21, 2008 Share Posted May 21, 2008 Sounds like you want template system: http://www.smarty.net/ Quote Link to comment Share on other sites More sharing options...
marcusfaye87 Posted May 21, 2008 Author Share Posted May 21, 2008 Sounds like you want template system: http://www.smarty.net/ Though I do want that I'm really looking for a tutorial of some kind for it, a clear tutorial, I read one before, and I still had no Idea what was going on. The engine will work fine for sure, but I want to create it myself so I'll learn from it and actually know what I'm doing. Thanks though Quote Link to comment Share on other sites More sharing options...
jonsjava Posted May 21, 2008 Share Posted May 21, 2008 I hate to sound so negative, but let me get this straight: You want us to help you code your own version of Smarty? That would be an arduous task for a dedicated team of decent programmers, and would take quite a while. You could always patch one together, using str_replace(), but what you are asking for sounds much bigger. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 21, 2008 Share Posted May 21, 2008 Smarty Tutorial: http://devzone.zend.com/article/1257-Smarty-A-closer-look A quick google search found this for basic templating using PHP: http://www.developertutorials.com/tutorials/php/php-template-script-050316/page1.html Quote Link to comment Share on other sites More sharing options...
marcusfaye87 Posted May 21, 2008 Author Share Posted May 21, 2008 I'm not asking for the help of a team, I was asking for a decent tutorial so I can do it on my own. The project itself wouldn't be so big, for example: I have a layout called "news_post.html". That contains the layout of the news post. And I just want to be able to replace certain things in it like: title, date, author, etc... Rhodesa, thanks for the tutorial, I'll look into it. but to further answer the question, I'm not the kind of guy that wants people to do stuff for me, I'm the kind of guy that likes figuring some stuff out on his own, too bad you can't do that without the knowledge of the tutorial So no offence taken Quote Link to comment Share on other sites More sharing options...
jonsjava Posted May 21, 2008 Share Posted May 21, 2008 a simple way would be to do something like this: (example template file) <?php echo <<<END <html> <head> <title>$title</title> </head> <body> $body_text </body> </html> END; ?> and then just do this for the files that you want to display your template: <?php $title = "this is a test title"; $body_text = "this is a test body text"; include("template.php"); ?> Quote Link to comment Share on other sites More sharing options...
marcusfaye87 Posted May 21, 2008 Author Share Posted May 21, 2008 I know, but I rather have my PHP sepperated all the way, I'm already doing it that way, but it's quite sloppy, Mainly because it has to happen in a while loop and such. Quote Link to comment Share on other sites More sharing options...
marcusfaye87 Posted May 21, 2008 Author Share Posted May 21, 2008 home.php <table width=700 cellpadding=0 cellspacing=0> <?php // Select the news posts $selectData = select_data ("news_posts", "id", 3); // Loop through the posts while($newsPost = mysql_fetch_array ($selectData)) { // Select the news post and turn it into an array $postRequest = select_data ("news_posts", "id", '5'); $postData = mysql_fetch_array ($postRequest); // Look for the author $postUserRequest = compare_data ("members", "id", $postData['user_id']); $postUserData = mysql_fetch_array ($postUserRequest); // Format our time for display $postData['date'] = format_date ($postData['date_created'], 'date'); $postData['time'] = format_date ($postData['date_created'], 'time'); // Set the users title $postUserData['title'] = set_user_title ($postUserData['rank']); // Check for Commens $postCommentsRequest = compare_data ("news_comments", "post_id", $postData[id]); $postData['comments'] = mysql_num_rows ($postCommentsRequest); // Echo the news Post echo " <tr> <td id=menu>$postData[topic]</td> <td> </td> </tr> <tr> <td id=title>[ $postData[time] ] - $postData[date]</td> <td id=titleRight>Author info:</td> </tr> <tr> <td id=post>$postData[body]</td> <td id=postRight> <img id=avatar src=\"avatars/$postUserData[avatar].png\"><br> <a href=\"?view=profile&&user=$postUserData[username]\">$postUserData[username]</a><br> <br>$postUserData[title]<br> <img src=\"template/images/rank/$postUserData[rank].png\"><br> <td> </tr> <tr> <td id=comment> <a href=\"?view=post&&id=$postData[id]\">Comments ($postData[comments])</a> </td> <td> </td> </tr> <tr> <td colspan=2> </td> </tr> "; } ?> </table> just added this so you get where I'm at Quote Link to comment Share on other sites More sharing options...
marcusfaye87 Posted May 21, 2008 Author Share Posted May 21, 2008 Smarty Tutorial: http://devzone.zend.com/article/1257-Smarty-A-closer-look A quick google search found this for basic templating using PHP: http://www.developertutorials.com/tutorials/php/php-template-script-050316/page1.html this should suffice. The last tutorial I read was with OOPHP and I had no idea what was going on 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.