Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What is the industrial practice on how to separate html and php code in a web project? Using echos to generate html is considered bad but what is the standard way to achieve the sought separation? Using template engines like twig?

share|improve this question

closed as too broad by gnat, GlenH7, World Engineer Apr 8 at 0:14

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask –  gnat Apr 6 at 12:46
1  
Not exactly a duplicate question but has very big overlap where I wrote about some pieces of this question: programmers.stackexchange.com/a/234745/46605 –  Luc Franken Apr 6 at 15:22

2 Answers 2

The general practice is to put HTML in a template.

A template contains minimum programming code: it's mostly HTML, with calls to variables and basic loops and conditional statements. The reason for that is that putting too much programming code in templates would:

  • Make testing difficult,

  • Make the reading and changing of the template difficult: the template should be straightforward enough to be able to be modified with minimum risk of introducing bugs.

A template engine, such as Smarty, is then used to transform the template into real content.

share|improve this answer

There are several aproaches, the one hand, using templates from the server side like Smarty, Twig, etc. I don't recommend this approach because you're coupling your presentation code to the technology used in the server (like php, python, ruby, java, ...).

The other approach is to use client side templates/frameworks for the view (like ember, angularjs, backbone, ...). I recommend this approach due to the separation of concerns between view (html,css,js ...) and server code or controllers (php/symphony, python/django, ruby/rails, java/spring ...).

So between your server code and your client code, the uniq glue you've to put is the model object in Json format (or whatever format you need). This way, you can develop code in the view that is reusable by any kind of server code, and thus, you can develop your application logic with different server technologies.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.