Best way to separate HTML and PHP?

What’s the best way to separate PHP Code and HTML?

You could keep the two completely separate by using a template language such as twig or smarty, but to be honest there’s nothing wrong with using PHP in your views to output variables and do simple looping.

It depends on how far you want to go down the rabbit hole, and how far down you already are.

If you can post an example of the kind of code you currently deal with, you’ll get a very good answer. Other than that, it’s down to mere ‘coding philosophy’.

PHP began life as a template engine. Hence, there’s no practical way to accomplish this way. There are a lot of stupid ways to do it, twig and smarty being two of the most popular, but I just don’t get it personally. What’s next, running my website on a virtual machine of Linux running in a virtual machine of Windows running on a x86 processor emulator for the Power PC architecture in order to get the page speed down to one page served every 2 hours? Is that the goal.

Ok, I’m exaggerating (a lot), but seriously PHP isn’t meant to be separated from HTML in any real sense. You can, and should, separate business logic from display logic - but that isn’t the same thing as separating the code types. If you really want to go that route then write your program in C and use PHP just as the template engine. Or use python or node.js.

I’d definitely agree here. PHP works fine as a templating language, and any deviation from that always looks ugly to me.

Indeed. If it WERE, then where on earth would the output get it’s data from?

I think the question is, rather, aimed at separating processing from output. This is a fairly simple task, but opinions often clash when it comes to implementation. If they didn’t, I’d fear for the role creativity has to play in the game of code.

PHP may have started it’s life as a templating language, but it hasn’t been evolving as one. We’ve gotten namespaces and late static binding and closures and traits and a butt-load more. Yet PHP’s templating syntax and features have received virtually zero enhancements

For what it’s worth, the motivations behind libraries like Twig include:

  1. More concise.

<?php echo $var ?>

  • vs -

{{ var }}

(Most projects consider PHP’s shorthand <?= to be unsafe because short_open_tag is often off.)

  1. Enforce separation of concerns.

PHP templates require self-disciple to not do more than you should in a template. Libraries like Twig make it impossible to do more than you should. This is especially good when designers (non-programmers*) start working on the templates.

*And let’s face it, even actual programmers will botch this sometimes.

  1. Template inheritance

It’s a cleaner, more reusable, more OO way to include common template code. It’s nice. :slight_smile:

  1. Escape by default

<?php echo htmlspecialchars($var, ENT_QUOTES, ‘UTF-8’) ?>

  • vs -

{{ var }}

This is tremendously good for security. It’s the “secure by default” approach.

Okay, I’m impressed!

So, back to the OP: Are you looking for application flow help, or are you looking for templating languages?

I don’t think the OP is looking for anything. Looking at his forum activity he posts very general, short questions and doesn’t really take part in any further discussion. His behaviour is half human, half bot-like and I think moderators should look into that. It’s sad because some good discussions start evolving out of those topics when some users are trying to be helpful…

There are templating engines such as smarty and twig for you, but first thing first you need to know how to separate business logic from presentation. Once you understand the basics of layers, using templating engines will truly help achieving a better separation, otherwise you won’t achieve good separation of concerns anyway.

Someday you really need to take a few hours out of your busy life and download the Symfony 2 AcmeDemo application. You might be surprised at what those French guys have come up with.

As of PHP 5.4 the short_open_tag directive has no bearing on the available of <?=, it’s always available. And even if you run and older version of PHP, Many of those same projects are written to require mod_rewrite in an .htaccess file, and it is IMPOSSIBLE to have access to the .htaccess directives and not have the ability to turn php short tags on using it if so desired. So, like many things in the PHP world, the prevailing opinion is born of ignorance.

  1. Enforce separation of concerns.

PHP templates require self-disciple to not do more than you should in a template. Libraries like Twig make it impossible to do more than you should. This is especially good when designers (non-programmers*) start working on the templates.

Never underestimate the ingenuity of idiots. Tying their hands with a template engine will just lead to them injecting the business logic somewhere ELSE it’s not supposed to go. Trying to force people to program well is a fool’s errand.

  1. Template inheritance

It’s a cleaner, more reusable, more OO way to include common template code. It’s nice. :slight_smile:

You can do this without a template engine if you structure your code and templates correctly.

  1. Escape by default

<?php echo htmlspecialchars($var, ENT_QUOTES, ‘UTF-8’) ?>

  • vs -

{{ var }}

This is tremendously good for security. It’s the “secure by default” approach.

Again, there is no reason the template handler can’t do this.

I have. A load of trash that takes 10 times longer to parse than what I wrote in straight PHP for the same use case. 5x longer to parse if I set Symfony 2 to use PHP as the template engine.

Great. Any chance of posting a link to your repository?

Thanks. My apologies for being unclear. What I really wanted was to look at your source code for your version of the Symfony 2 AcmeDemo application.

Being able to speed up parsing times by a factor of 10x sounds very impressive. Even if I have no idea what that means.

Hi Michael - I agree, I’ve tried various engines and personally I find my own simplified templating engine is far more transparent to understand, maintain and quicker.

However - further to the topic thread. I use a sort of MVC model of my own code where I have a controller to prepare objects and settings which rely on classes to generate the data from DB or similar and pass these over to a template to merge the data in with the HTML. Alot of the functionality I provide is delivered using AJAX and therefore small templates with specific functionality. To give an idea of scope - I currently have 180 template files in one project I’m working on.

I’d appreciate your thoughts on

a) Javascript tends to be needed by these smaller pieces, which is specific to those areas. Do I have 1 (currently 8 in total) large’ish javascript files containing code which most of the time wont be referenced - but might be easier to compress/maintain and just a few calls - or - include the Javascript in with the template and reduce calls, but makes it really difficult to compress and exposes my javascript for the curious (no great harm but a consideration). Or have loads of small javascript files that get pulled in along with the ajax calls.

b) kind of related, CSS. I try and use common CSS as much as possible and keep it on 2 or 3 CSS files. These are pulled in for page refreshes. However there are a large number of times when styling is only applied to a few elements and creating/compressing/mainting a CSS file for these seems an overhead rather than using inline CSS.

Appreciate your thoughts…

Richard.