需要一些使用PHP / HTML进行模板化的建议

I read this post and I really like this solution to templating but I am unsure of one thing. With this system wouldn't the business logic and presentation logic be in two different php files? This is all well and good as it separates them but what if there is only presentation logic as there are on some pages or what if there is a very low amount of business logic?

It seems weird that the default page the users page to will sometimes be only presentational logic and sometimes only business logic. It seems like there are two obvious solutions to this:

1) Have all default pages have business logic (even if there is none) that link to the presentational logic on a different page. The problem with this is that there are then a lot of "unnecessary" pages. The good is that it is consistent.

2) If there is no business logic for a page then just only include the presentational logic. The problem with this is that it is inconsistent when looking at filenames as to what php page includes the business and presentational logic.

Also, and this may be a little off-topic, but is there any way to template this?

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>My Website</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <link href="styles.css" rel="stylesheet" type="text/css"/>
        <link href="favicon.png" rel="shortcut icon" />
    </head>
    <body>
    </body>
</html>

Considering I have this on every page I was wondering if there was a way to template this so the code could all be in one file and recalled.

Your templates should contain strictly presentational logic.

This means that you can include:

  • Loops (to print lists etc)
  • Conditionals (e.g. only print an element if some value is non-null)
  • Formatting functions (strip spaces, round numbers, etc)
  • A small number of template-composition helper function calls (e.g. "please include this other sub-template here")

but nothing else -- and especially not business logic! (if I forgot something important, please mention it in a comment)

Also, your templates ("views") should never be used as the target URLs that you application uses. You should have the URLs point to (possibly one of many) "controller" scripts, which then invoke the necessary business logic and pass the results to your template for display by including the template. You will find this easy to grasp if you are familiar with Model-View-Controller; if you are not, familiarize yourself first.

Finally, here's one way you could template the markup you gave:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title><?php echo $title;?></title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<?php foreach ($stylesheets as $stylesheet) : ?>
        <link href="<?php echo $stylesheet;?>" rel="stylesheet" type="text/css"/>
<?php endforeach; ?>
        <link href="favicon.png" rel="shortcut icon" />
    </head>
    <body>
    </body>
</html>