Ricardo Binns
Ricardo Binns

Reputation: 3246

what is the best way to keep my php files with only php code's?

I have a file with php and html codes mixed, like this:

echo "<tr class='somthing'>";

What is the best way to keep my php files with only php code?

Upvotes: 5

Views: 425

Answers (6)

George Cummins
George Cummins

Reputation: 28906

You should examine the MVC (model-view-controller) approach. MVC allows you to separate your business logic from your presentation logic (HTML).

For an overview of MVC, see: http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

For a list of PHP frameworks that support MVC, see: http://www.phpframeworks.com/

Upvotes: 1

onteria_
onteria_

Reputation: 70487

One way would be to utilize a templating system like Smarty as others have mentioned. I wrote somewhat of an introduction to it in another question:

Beginner's guide to making a template driven PHP site

As you can see, it provides a great way to keep your HTML and PHP code separate

Upvotes: 1

Dereleased
Dereleased

Reputation: 10087

There is some contention about this, so I will simply say that whatever you choose, stick with it.

Generally, you can use a templating system (e.g., Smarty) to keep your files formally clean of PHP code, but this has some drawbacks in the speed department.

Alternatively, you can use minimal code by breaking out the display related portions and using small PHP snippets therein, e.g.

<?php foreach ($myArray as $myElement): ?>
<tr>
    <td><?php echo $myElement; ?></td>
</tr>
<?php endforeach; ?>

Of course this doesn't completely separate your code from your HTML, but that's nearly impossible to do for anything nontrivial; in my opinion, Smarty templates are just different code, not the absence of code. This approach is advocated by some frameworks such as codeigniter and Zend Framework (thought you can use them with Smarty as well, it is not the default).

One important thing to remember is that your display logic is just that: logic. Whatever method you choose, you will never be 100% free of code (or code-like markings which are interpreted by other code/applications) in the display until you use static-only displays.

Upvotes: 4

James C
James C

Reputation: 14149

It's good to keep "data" and "display" logic separate. One nice way of doing this is to use an architecture called Model, View, Controller (MVC)

Most PHP frameworks are built around the MVC. Have a look at Zend Framework or Codeignitor.

Upvotes: 2

jlindenbaum
jlindenbaum

Reputation: 1881

You could go to a template engine like Smarty. This allows you to have a controller to prepare all your data, then pass it off to a view for rendering.

You can also do it yourself. You have a view that simply uses or basic loops to echo out rows. Strict outputting, no other logic. Your controller prepares the data, then you can use output buffering to include the view, have it render, get the buffered output and echo it at a later point.

Upvotes: 2

mcgrailm
mcgrailm

Reputation: 17640

use a templating engine such as smarty

here is a list of others

Upvotes: 1

Related Questions