owner Posted September 30, 2007 Share Posted September 30, 2007 Hello, I don't know if this is even possible, but I was wondering if their is a better way to be doing things when displaying php and html code. (This might be considered using OOP as I am trying to convert from normal php coding to OOP.) Here is a snippet off my news code that I have right now. <div id="content"> <?php do { ?> <div class="boxtop"><h4> <?php echo $row['news_title']; ?> </h4></div> <div class="boxbottom"><br /> <?php echo $lang_id . $row['news_id']; ?><br /> <?php echo $lang_author . $row['news_author']; ?><br /> <?php echo $lang_date. $row['news_date']; ?><br /> <?php echo $row['news_body']; ?></div> <?php } while ($row = mysql_fetch_array($result)); ?> </div> Now you can see in the code above, im just grabbing some info out of the database and looping this a few times to show new blocks for my news. Anyways, I was wondering if you could make a file like news.php and then inside you would have something like this. <?php function news($new_title="") { <div><b>{$news_title}</b></div> } ?> That way, I can just grab this function and loop that instead of mixing a ton of html and php in my source (This sucks right now because in a login system, the html just gets in the way big time). Please put up any pointers in the right direction. (Note: I am just starting to learn the basics of php and php using OOP, so please dont beat me up with a stick for not knowing a lot of stuff ) Thanks in advance! -Owner Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/ Share on other sites More sharing options...
pocobueno1388 Posted September 30, 2007 Share Posted September 30, 2007 You could resort to OOP, but that all depends on how big this script is. If it isn't very big, I would just write a simple function. <?php function display_news(){ //query database while ($row = mysql_fetch_assoc($result)){ <div class="boxtop"><h4> <?php echo $row['news_title']; ?> </h4></div> <div class="boxbottom"><br /> <?php echo $lang_id . $row['news_id'].'<br />'; echo $lang_author . $row['news_author'].'<br>'; echo $lang_date. $row['news_date'].'<br />'; echo $row['news_body'];</div> } } ?> Now, wherever you want to display the news, all you have to do is display_news(); Obviously that function needs to be changed up depending on how you want it to work. You could add parameters to the function to only display a certain amount of results, or whatever. Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/#findComment-358185 Share on other sites More sharing options...
owner Posted September 30, 2007 Author Share Posted September 30, 2007 Hello, That isn't what I really want to do. I really want to convert to OOP as I might recall the functions many times and it would be a lot more sorted. What I want to do is use code like <?php function news($new_title="") { <div><b>{$news_title}</b></div> } ?> and have the {$news_title} be replaced. A big example of what I want to do is to be able to load the templates like forum software does. That way you can sort your php scripts from your html data. Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/#findComment-358269 Share on other sites More sharing options...
owner Posted September 30, 2007 Author Share Posted September 30, 2007 May I bump? Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/#findComment-358455 Share on other sites More sharing options...
BlueSkyIS Posted September 30, 2007 Share Posted September 30, 2007 are you looking for echo? return? I'm with the "don't use OOP unless there is a compelling reason to do so" crowd. i use the same functions over and over and over again but I have no objects. <?php function news($new_title="") { echo "<div><b>$news_title</b></div>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/#findComment-358488 Share on other sites More sharing options...
owner Posted September 30, 2007 Author Share Posted September 30, 2007 Sorry, I was wronge with that code. This is what I really want to do. <?php function show_row($session="") { $html = ""; $html .= "<tr> <td class=\"row1\">{$session['member_name']}</td> <td class=\"row1\" align=\"center\">{$session['msg_icon']}</td> </tr>"; return $html; } ?> See, you would store a ton of functions and then call each block of code. And then as you see, the variables will be replaced with the members actual name, the time, etc. See what I am trying to do? (Note: This little section of code was taken from the bb software IPB.) Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/#findComment-358536 Share on other sites More sharing options...
xylex Posted September 30, 2007 Share Posted September 30, 2007 If you build out your functions to include HTML formatting and the variables, you're just going to be making a lot more work for yourself in the long run. In your example show_row() function, what if you wanted to display that outside of a table, or in a table that had something other than two columns? You'd have to create a new function for every variation you used, making your code a lot less reusable, harder to read, and longer to write and update. If you want to make your own templating class, make the functions either insert HTML around variables, or get and clean the variables for display, not both. Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/#findComment-358556 Share on other sites More sharing options...
owner Posted September 30, 2007 Author Share Posted September 30, 2007 If you build out your functions to include HTML formatting and the variables, you're just going to be making a lot more work for yourself in the long run. In your example show_row() function, what if you wanted to display that outside of a table, or in a table that had something other than two columns? You'd have to create a new function for every variation you used, making your code a lot less reusable, harder to read, and longer to write and update. Ok, I see what you are saying here. If you want to make your own templating class, make the functions either insert HTML around variables, or get and clean the variables for display, not both. I don't get what you mean here. Could you show me an example? Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/#findComment-358558 Share on other sites More sharing options...
chocopi Posted September 30, 2007 Share Posted September 30, 2007 I think you want something like phpbb so in the template file they have <div>{L_SOMETHING}</div><h1>{L_SOMETHING_OTHER}</h1> then in the php files $template->assign_vars(array( 'L_SOMETHING' => $lang['Something'], 'L_SOMETHING_OTHER' => $lang['Something_Other']) ); And then in the language files $lang['Something'] = 'Some Text'; I think you want something like that. And just for the record I was gonna ask in the freelancer on how to do this ~ Chocopi $lang['Something_Other'] = 'More Text'; Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/#findComment-358563 Share on other sites More sharing options...
owner Posted September 30, 2007 Author Share Posted September 30, 2007 that is exactly what I am looking for!!!! Thank you very much, but I don't understand it 100%, would you mind explaining it a little. I am pretty new with php so I am dumb with the coding a little. I have my language file with <?php $lang = array( 'Something' => 'Some Text', 'Something_Other' => 'More Text' ); ?> and my index file that will display everything with this code <?php include "./lang.php"; array('L_SOMETHING' => $lang['Something'],'L_SOMETHING_OTHER' => $lang['Something_Other']) ?> <div>{L_SOMETHING}</div><h1>{L_SOMETHING_OTHER}</h1> Yet, its not replacing yet Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/#findComment-358571 Share on other sites More sharing options...
chocopi Posted September 30, 2007 Share Posted September 30, 2007 well seeing as your not using any functions such as str_replace, preg_replace etc nothings going to happen. But there must be a more efficient way of doing this. ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/#findComment-358574 Share on other sites More sharing options...
owner Posted September 30, 2007 Author Share Posted September 30, 2007 Would you mind adding me to msn and helping me out? If not that is ok to, but would you mind helping me out on here then? Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/#findComment-358575 Share on other sites More sharing options...
xylex Posted September 30, 2007 Share Posted September 30, 2007 Why don't you just an existing templage engine like Smarty? It already does everything that you're looking to write. Google it, there are tons of tutorials to get you going. Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/#findComment-358600 Share on other sites More sharing options...
owner Posted September 30, 2007 Author Share Posted September 30, 2007 I dont want to use smarty, I want to learn by writing the code instead of using that. Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/#findComment-358638 Share on other sites More sharing options...
owner Posted October 1, 2007 Author Share Posted October 1, 2007 May I bump again? Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/#findComment-359015 Share on other sites More sharing options...
chocopi Posted October 1, 2007 Share Posted October 1, 2007 To be honest I don't think someone is going to spend the time writing the script and helping you, seeing as it wouldn't be an easy job and it would be very time consuming when there are other users who need help. I just googled it and though I haven't completly looked through it you should give this a try: http://www.pixel2life.com/publish/tutorials/457/simple_template_engine_find_and_replace_variables/ EDIT: I just downloaded it and the code works, apart from you have to make 2 changes. So I suggest you try this out as it seems to be a very useful resource Good Luck ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/71209-dynamic-templates-with-php/#findComment-359372 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.