Alex Rivera | Logout

Preferred way to combine PHP and HTML?

Asked 2010-07-10T20:01:08.353
9

I learned PHP by hacking away at phpBB2, even submitting a few mods to their database, which others downloaded and used. (I don't believe phpBB2 is supported any more with phpBB3 out so long now, so the v2 mods database is no more.)

One of my favorite things about phpBB was their templates system, which let the editor completely separate the HTML and the PHP. PHP files contained PHP: logic, database queries, and activating the templates. TPL files contained templates: HTML, template variables, and specialized HTML comments to allow for conditional or repeating blocks.

However, any time I see someone's PHP code online, it's either a small snippet working with a single function or such, or the PHP is full of strings containing HTML (or worse, HTML with PHP interspersed). phpBB is the only PHP I've looked at which actually separates the language and the markup language, suggesting to me that few, if any, other PHP codebases do such a thing.

I'm looking to start working with some PHP again, but this time it won't be a phpBB forum, and it will be on a team. Based on my experience, separation of PHP and HTML is uncommon (please correct me if I'm wrong on this!). However, I'm so used to that dividing line, I hate reading mixed PHP and HTML.

In the "real world" of PHP programming, what's the preferred method:

  • PHP files with strings of HTML
  • HTML files broken up with PHP blocks
  • PHP and HTML completely separate (I wish?)
  • Something else?
Edit
Report

1 Answer

3

any time I see someone's PHP code online, it's either a small snippet working with a single function or such, or the PHP is full of strings containing HTML (or worse, HTML with PHP interspersed).

Yeah, the general quality of code out there is embarrassing.

Whilst you can go to a full-blown templating engine of your own, that may be overkill and in many cases the limited domain-specific-languages they provide will get in your way, when you could just be writing presentation logic in PHP.

If you want to still be writing normal PHP, but without the spaghetti:

  1. Keep action code at the top of the file or in a different file. Put only application logic here, not any kind of HTML or templating. Any information generated in this stage that needs to be displayed by the template needs to go in a variable to be passed to the templating part, not print​ed out in the middle of a load of business logic.

  2. Take your knowledge of tag-based templating and apply it to PHP. Have a single, correctly-indented hierarchy of code for both HTML and PHP presentation logic, as if you were writing ‘well-formed’ XML (whether or not you're actually using XHTML). Avoid putting HTML in strings at all costs.

  3. Define an easier way of calling htmlspecialchars(), because otherwise typing that all the time is going to be a real pain, and if you aren't typing it all the time you're going to have potentially security-sensitive errors.

To summarise, eg.:

<?php
    // active code here
    // do things
    // put any errors in a $errors array

    // this trivial function would presumably be in an include
    //
    function h($s) {
        echo htmlspecialchars($s, ENT_QUOTES);
    }
?>

<body>
    <?php if (count($errors)!=0) { ?>
        <ul id="errors">
            <?php foreach ($errors as $error) { 
answered 2010-07-10T22:04:02.773

Your Answer