PHP and HTML codes are all messed now in my application. What should I do? Something like template engine? I want to program it myself. It is with purpose to learn PHP.
-
2Perhaps you can expand a little on "all messed up". – Brian Ortiz Dec 25 '10 at 01:41
-
sorry but this is not a good question..it doesn't make sense – rabidmachine9 Dec 25 '10 at 01:42
-
1If you can't understand the question — don't say it doesn't make any sense. – tomsseisums Dec 25 '10 at 01:44
-
@Brian Ortiz, I assume the OP is referring to the stereotypical PHP file that has application logic mixed in with HTML. It seems like a perfectly clear question to me. – aaronasterling Dec 25 '10 at 01:53
-
2Please don't close. It's true that it is not a real question, but it is clear what he wants. – Dec 25 '10 at 01:57
-
It could be a wiki. I don't mind about the points. – ilhan Dec 25 '10 at 02:01
4 Answers
Here's my view rendering function.
<?php defined('APPPATH') or die('no direct script access');
class ViewFileMissing extends Exception {}
function renderView($viewname, $data)
{
$filename = APPPATH.'/views/'.$viewname.'.php';
if (!file_exists($filename))
{
throw new ViewFileMissing($viewfile);
}
extract($data, EXTR_SKIP);
ob_start();
require($filename);
return ob_get_clean();
}
It can be used with a template like
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<p><?php echo $paragraph; ?></p>
</body>
</html>
It can then be called like
$data = array('title' => 'This is a title', 'paragraph' => 'This is a paragraph')
$html = renderView('viewname', $data)
It works by creating a local variable for each of the elements of the data array, turning on output buffering, requiring the template file, which outputs all of the html into the buffer and then clearing the buffer and returning the output. So all of the rendered html is now in a string that can be echoed.
The html is just for example, it wouldn't validate. Also note that this is the only legitimate use of extract
that I have ever seen.

- 68,820
- 20
- 127
- 125
You could use MVC.
M - Models
Specifies the data to be shown and/or handled by the views and controllers.
V - Views
This is your actual HTML with the only PHP is the code that replaces itself with the data provided by the controller.
C - Controllers
Processes the browser's request, fetches the data from the model and passes it to the view.
Example
models/post.php - model - blog post (dummy data return, you could use a database, web service, whatever)
<?php
function post_with_id($id) {
$post = array();
$post["id"] = $id;
$post["title"] = "Sample post " . $id;
return $post;
}
post.php - controller - processes request, navigate to e.g. /post.php?id=3
<?php
require_once('models/post.php');
$post = post_with_id((int)$_GET["id"]);
include('views/post.php');
views/post.php
<h1><?php echo $post["title"]; ?></h1>
This really keeps your code clean. Just alter your model to fetch other data, from a database or from files for example and you have your dynamic content!
This is just a very simple example of MVC. Look at Wikipedia's article about it. Have fun!
-
That's PMVC at best. Model=Database and Views=Template and fat controllers means Passive-MVC. Also, while it's a misnomer and no silver bullet, it's a practicable structure to separate concerns. Good examples. – mario Dec 25 '10 at 02:01
-
1
-
@ilhan there are MVC frameworks available for PHP. Take a look at CodeIgniter and CakePHP. :) – Jan 04 '11 at 12:39
Use MVC -- Model, View, Controller. Models contain application logic and database access, and overall data handling. Views contain HTML code and are like templates. Controllers are the URLs the user goes to, and include the models to retrieve/set the data and the views to display it.
Maybe look at some applications that use MVC and see how they're structured.

- 2,394
- 1
- 23
- 29
-
MVC [doesn't really work](http://stackoverflow.com/questions/4528272/is-mvc-now-the-only-way-to-write-php) on the web; it's a misnomer and framework buzzword. All PHP frameworks implement a fake version of it. Also your described structure resembles "PMVC" at best. - It's perfectly fine as hypothetical code organization scheme, of course. – mario Dec 25 '10 at 01:55
-
Sure it does. If you want to use it to the letter, you can always use XSLT. – dionyziz Dec 25 '10 at 19:42
The simplest, and fastest, way to separate your presentation (HTML) from your logic (PHP) would be to use Smarty Templating Engine. It's a half-step between running hybrid files (where the logic and the presentation are mashed together) and a fuill MVC solution. But I would think that it would be easy to go that way, should you want to down the track.

- 10,357
- 2
- 26
- 41
-
There are lines you need to draw when "programming it yourself". Using frameworks, classes and components available online and as Open Source, to create your own system is fine - you don't need to (and often shouldn't) build everything from scratch. After all, you are using PHP - unless you are actually rewriting PHP and the underlying OS in machine code, everything you make is reliant on the work of someone else. – Luke Stevenson Dec 25 '10 at 02:24