开发者

What are the differences between framework and template engine?

What开发者_开发知识库 is the difference between frameworks like zendframework and a template engine like smarty?

Which should I start with as a beginner?


Template engines are there to make web designer's life easier. Frameworks are for programmers.

Frameworks may contain one or more template engines. Since frameworks are for programmers, new or the programmer's own template engine may be embedded into the framework.

As a programmer who doesn't have to work with a designer, in PHP you don't really need a template engine, since PHP itself may be surrounded by (X)HTML code.

Creating a template engine, as a programmer

As proof to the fact that PHP itself can be used as a templating engine, here is how you can sepparate business logic from view logic.

It's a home-backed dummy templating engine. It is not complete, it is not safe. It'S only a prototype to show you the basic idea of templating.

You have probably already heard about MVC - or not - it doesn't matter. The practice described below is similar to it, but you don't have to program OOP or use a framework

Your "views" are simply templates which get some variables from your script. In the main script (here greet.php), you only do the "business logic". "Business logic" includes all database operations, working with sessions, doing all the maths and checking for valid input, eventually filtering it.

Then all you have to do is to store the data which you want to be displayed in intermediate variables. In the example below, that's $title, $name, $showdata and $errors.

The function render() does one important thing: it isolates the template about to be included from the outer world of the business logic of our script by using the automatic scope of variables - the variables extract()'ed from the associative array are local to render() - the entire template exists only within that function.

Please note that the variables extract()'ed are named after the associative indexes of the second parameter to render(). If your template doesn't require different names for variables, then you could cut some lines by initializing the array like this:

$export = compact('title','name','showdata','errors');

The variable $do_greet will no longer exist inside your template. Instead the same variable will be known under the same name as in your business logic script, namely $showdata.

greet.php

<?php
$title = 'Contact';
$name = 'Guest';
$showdata = FALSE;
$errors = array();
if(isset($_POST['submit'])) {
        if(isset($_POST['name']) && $name = trim($_POST['name'])) {
                $name = strip_tags($name);
                $showdata = TRUE;
        }
        else {
                $errors[] = 'Missing name.';
        }
}
$export = array(
        'title' => $title,
        'name' => $name,
        'do_greet' => $showdata,
        'errors' => $errors
);

render('greet_view.php',$export);

function render($template,$data) {
        extract($data);
        return include $template;
}

One important note on templates like this: if you try to access global data, database connections, superglobal arrays like $_SESSION, $_GET, $_POST, $_COOKIE, $_FILES, $_SERVER, etc, in your templates, then you're not using this technique properly. Your aim is to separate completely the logic from the view.

If you do need such data, then make it available to the view through an intermediary variable, for example:

$export = array(
        'title' => $title,
        'name' => $name,
        'do_greet' => $showdata,
        'errors' => $errors,
        'referer' => htmlentities($_SERVER['HTTP_REFERER'])
);

Here is the code of the view or template greet_view.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">                                                                                  
<head>
        <title><?php echo $title ?></title>
</head>
<body>
<?php
if($do_greet) {
        echo 'Hi ',$name;
}
if(count($errors)) {
        if(count($errors)>1) {
                echo '<p class="error">',implode('</p><p class="error">',$errors),'</p>';
 }
 else {
                echo '<p class="error">'.$errors[0].'</p>';
 }
}
?>
<form method="post">
        <input name="name" />
        <input type="submit" name="submit" />
</form>
</body>
</html>

Disclaimer: the presented code is not clean, nor is it safe or perfect. My intention is only to put you on the right track. It is your job to do more research.


The framework

That was the template engine part. A framework provides functionality (in form of functions and/or classes) to solve common problems like authentication, authorization, routing a request to the proper file/class (controller in the MVC world), and so on.

This functionality, unlike in a CMS, it's not ready to use as-is. Different components of the framework must be wired together by the programmer. Because the programmer only needs to do this wiring, but not (re)write that functionality over and over again for each and every project, a framework makes programming more enjoyable, letting the programmer to concentrate on the actual project-specific problems.

A template engine like the one presented above could be part of that framework, and the render() function could be a method of the controller (in MVC terms).


Frameworks are more complex than template engines. A framework can contain a template engine but not reverse. A framework can help you in numberless ways to build your (web) application. A template engine is simply used to parse variables into your preformatted html template.


In short:

A template engine does nothing more than a little more advanced str_replace. It searches for special tags in the template and replaces them with the corresponding values. More advanced engines (like Smarty) also have loops and conditions.

A web framework (like Zend) does most of the core stuff of a webpage, taking page-requests and forwarding them to the handlers responsible for processing the request and usually also forwarding the response information to a template engine for seperation of logic and layout. Web frameworks (there are other framework types, i.e. GUI Frameworks, Networking Frameworks, Graphics Frameworks, etc.) usually have utility helpers that help you with everything a typical web site needs, Form verification, Session management, Captcha generation, URL rewriting, ...


A framework like Zend Framework is essentially a extension on top of PHP, while Smarty is a template engine that simply separates all your PHP variables from the front-end (html), while also supporting simple methods of caching content.

I'll recommend you to start learning Smarty for the simplicity of it, and you'll be surprised how much more easier it will be combining your front-end design with your PHP code.

You can also combine Smarty with eAccelerator http://eaccelerator.net/ to speed up things a bit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜