tags:

views:

511

answers:

9

What is a good way to remove the code from display pages when developing with PHP. Often the pages I work on need to be editted by an outside person. This person is often confused by lots of blocks of PHP, and also likes to break my code.

I've tried moving blocks of code out into functions, so now there are functions spread out all throughout the HTML now. As some pages become more complex it becomes a program again, and processing POSTs are questionable.

What can I be doing better in my PHP development?

A: 

There's a lot that can be said on this topic but a very basic starting point would be to move as much code as possible out into separate files and then use include statements.

Mark Biek
+5  A: 

Take a look at how some of the popular PHP frameworks use templating. Examples include cakePHP, Zend Framework, and Code Igniter. Even if you are not going to base your site on these frameworks, the template design pattern is a good way to keep php code away from your web designers, so they can focus on layout and not functionality.

Christian Oudard
A: 

I usually use includes, as they can be very useful for organising and grouping functions together. Also, comment your code. There's nothing worse than for someone else to see your work and not know why you've done this. Naming variables and functions sensibly can go a long way too - for example:

$userName = "John Doe";
$dateOfBirth = "04/02/1982";

function calculateUserAgeFromBirth($userName, $dateOfBirth)

Naming variables like this also helps minimise comments about what your code actually does.

different
+2  A: 

Does the outside person need to edit the logic, or just the display (HTML)?

If it's the latter case, check out the Smarty template engine.

Anonymouse
+13  A: 

You don't need a "system" to do templating. You can do it on your own by keeping presentation & logic separate. This way the designer can screw up the display, but not the logic behind it.

Here's a simple example:

<?php 
$people = array('derek','joel','jeff');
$people[0] = 'martin'; // all your logic goes here
include 'templates/people.php';
?>

Now here's the people.php file (which you give your designer):

<html> 
<body>
<?php foreach($people as $name):?>
   <b>Person:</b> <?=$name?> <br />
<?php endforeach;?> 
</body>
</html>
lo_fye
+1  A: 

I think I'd like to stay away from an unweildy framework. Just some approach I can use that generally makes the pages more readable with cleaner code.

Stack Overflow wants me to decide which answer is best, when best is a subjective opinion. Who is to say what the 'best' practice is.

Jack B Nimble
+1  A: 

If you decide to continue using functions, you can get some inspiration from WordPress. You can probably reduce the "program" to a minimum by making templates more granular.

Also, good tools (i.e. HTML editors) can help designers ignore your PHP and work on the design without breaking the code. (But I have no suggestions, sorry.)

The other way to some things is to create own template system instead of SMARTY, but it would probably take too long to create a working system to satisfy your needs that would go past just a replacing something like %%VARIABLE%% with a text.

Our company uses SMARTY and even with a lot of code in templates, designers know how to work with it. For simple CMS sites we use ExpressionEngine, which uses HTML-like tags for inserting logic into templates.

analytik
+2  A: 

It sounds to me like you need to begin implementing what is known as "separation of concerns" across your application generally. The examples folks give about templating, in response to your specific complaint about page editors breaking your code, are important, but represent just one example of this tactic. As your program gets larger and more complex it becomes harder to modify and debug--even if your designer is not breaking your code.

Probably the most common separation is a three way split between data, logic and presentation as described in the design pattern Model-View-Controller (MVC). You do not need a full blown MVC framework in place to implement the same basic principles. The idea is simply to encapsulate code that deals with your data (model) in one place, the code that presents this data to the user (view) in another. You tie that code together with code that is only concerned with presenting the right data to the right user at a the right time (controller).

From your description, it sounds like you have right now is a Transaction Script pattern, where you have a php file "dothis.php" that is loaded in the browser, and all the function definitions and HTML for the display are together. You already have functions, so you are already beginning to encapsulate pieces of logic.

The way I would approach this would be, in keeping with the other answers here about templating, is to remove all of the HTML into another file only referencing simple PHP variables and maybe some loops (but as little conditional switching as you can). That will make the template easier to read and harder to break. When your page editor wants to modify the layout, give them THAT file.

You then separate all of your data access functions to another file, ideally creating a class (or several classes, depending on how complex your data is and how frequently you need to reuse it).

At this point your "dothis.php" has been stripped down to maybe some configuration code (which you can separate out to an include, and some authentication code (which you can separate out to its own class), and is only calling the data access functions, and calling the included template file. Your controller itself is therefore greatly simplified and easier to manage.

Sam McAfee
+2  A: 

I would highly recommend reading the book PHP In Action. It takes you through abstracting your database connections, templating systems and all the other basics of a web application. If every PHP developer read this book then the language would have a much better reputation.

It also has chapters on refactoring, unit testing and the MVC control pattern.

David McLaughlin