Jump to content

PHP with HTML = Bad....How do I stop this habit?


3raser

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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? :/

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.