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.
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.
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!
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.
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.
精彩评论