3raser Posted March 8, 2011 Share Posted March 8, 2011 I have a habit of doing stuff like such: <div class="portal_box"> <h2>Welcome to ZombieCraft's Highscores!</h2> <div class="portal_content"> Here you can sort the pros from the noobs, or just simply see whos on top of their game. The highscores will show the highsest scores, greatest to least. If you want to see a certain user's score, then just use the search button! <?php $username = $_SESSION['loggedin']; $extract_user_rank = mysql_query("SELECT rank FROM users WHERE username='$username'"); $grab = mysql_fetch_assoc($extract_user_rank); if($grab['rank'] > 0) { echo "</br><br/><a href='control_panel.php'>Admin Control Panel</a>"; } else { //return nothing } ?> </div> </div> I've been told a good bit of times that I need to stop using HTML and PHP in the same lines. How would one accomplish this? Quote Link to comment https://forums.phpfreaks.com/topic/229940-php-with-html-badhow-do-i-stop-this-habit/ Share on other sites More sharing options...
btherl Posted March 8, 2011 Share Posted March 8, 2011 Using a templating system like Smarty makes it easy to keep the php and HTML seperate. It'll make both your php and HTML much easier to read. Quote Link to comment https://forums.phpfreaks.com/topic/229940-php-with-html-badhow-do-i-stop-this-habit/#findComment-1184305 Share on other sites More sharing options...
3raser Posted March 8, 2011 Author Share Posted March 8, 2011 Using a templating system like Smarty makes it easy to keep the php and HTML seperate. It'll make both your php and HTML much easier to read. Thanks, I'll take a look around at Smarty. Quote Link to comment https://forums.phpfreaks.com/topic/229940-php-with-html-badhow-do-i-stop-this-habit/#findComment-1184308 Share on other sites More sharing options...
trq Posted March 8, 2011 Share Posted March 8, 2011 Most template engines (including Smarty) are overkill IMO. You can easily create your own simple methods which will allow your business logic to be separated from your design. A little php amongst your HTML is harmless and is in fact quite a bit more efficient than any of the templating engines. See http://www.phpfreaks.com/forums/index.php?topic=320371.msg1510211#msg1510211 for a simple example. Quote Link to comment https://forums.phpfreaks.com/topic/229940-php-with-html-badhow-do-i-stop-this-habit/#findComment-1184311 Share on other sites More sharing options...
3raser Posted March 8, 2011 Author Share Posted March 8, 2011 Most template engines (including Smarty) are overkill IMO. You can easily create your own simple methods which will allow your business logic to be separated from your design. A little php amongst your HTML is harmless and is in fact quite a bit more efficient than any of the templating engines. See http://www.phpfreaks.com/forums/index.php?topic=320371.msg1510211#msg1510211 for a simple example. What do professionals use? And I appreciate that tutorial. I don't understand a view functions, but I'll just check the manual. So, officially, what is your opinion? Go through the template process, or just include PHP and HTML together? Quote Link to comment https://forums.phpfreaks.com/topic/229940-php-with-html-badhow-do-i-stop-this-habit/#findComment-1184314 Share on other sites More sharing options...
trq Posted March 8, 2011 Share Posted March 8, 2011 Template engine create an extra layer that isn't necessary. This layer not only slows down execution but creates a new 'language' that users must learn in order to edit your templates. Mixing PHP and HTML isn't bad as long as your not mixing the bulk of your logic into it, and using small amounts of php within html means that users will only need to know a small subset of php to be able to edit your templates. I am a professional PHP developer and would never consider a template engine. Quote Link to comment https://forums.phpfreaks.com/topic/229940-php-with-html-badhow-do-i-stop-this-habit/#findComment-1184316 Share on other sites More sharing options...
3raser Posted March 8, 2011 Author Share Posted March 8, 2011 Template engine create an extra layer that isn't necessary. This layer not only slows down execution but creates a new 'language' that users must learn in order to edit your templates. Mixing PHP and HTML isn't bad as long as your not mixing the bulk of your logic into it, and using small amounts of php within html means that users will only need to know a small subset of php to be able to edit your templates. I am a professional PHP developer and would never consider a template engine. Thanks Thorpe, highly appreciated. Also, what happened to the mark solved button? :/ Quote Link to comment https://forums.phpfreaks.com/topic/229940-php-with-html-badhow-do-i-stop-this-habit/#findComment-1184331 Share on other sites More sharing options...
KevinM1 Posted March 8, 2011 Share Posted March 8, 2011 More to the point, PHP IS a templating engine itself. Things like Smarty are ultimately redundant, especially if time and care are spent on figuring out how to efficiently display things. The general rule of thumb is to limit PHP code used for display to conditionals and loops. It should be fairly simple to accomplish if you structure your code properly (process THEN display). Quote Link to comment https://forums.phpfreaks.com/topic/229940-php-with-html-badhow-do-i-stop-this-habit/#findComment-1184354 Share on other sites More sharing options...
btherl Posted March 9, 2011 Share Posted March 9, 2011 I'm a professional php developer too and I consider Smarty to be as useful as OOP. It enforces seperation of your data manipulation code and your display code. Just as one of OOP's major benefits is actually that it restricts what you can do, Smarty also restricts you, forcing you to follow good coding practices. OOP => isolation of objects => unit testing, interface preserving changes do not introduce bugs because of isolation => good code Smarty => isolation of logic and display => simple logic code, simple display code, interface code can be changed in isolation of display code and vice versa => good code All of these come down to one principle - break the program down into simple (and isolated) pieces, and make sure each piece works, and that they are combined in a way that works. Then your whole program will work. Quote Link to comment https://forums.phpfreaks.com/topic/229940-php-with-html-badhow-do-i-stop-this-habit/#findComment-1184810 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.