The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Should I put html generating code in a PHP class?

I am writing a web application right now and I am trying to make it as modular as possible.
Of course there are parts of the code where a set of items are fetched from a db and the code loops to output each item to the screen.

Is it best to put this kind of code in the view/html page or in the controller/php class that I'm writing?
Tuesday, February 06, 2007
Almost always, the answer is "don't put your code and your HTML in the same place."

Your view class should be passing the data into a template. In the template, loop over the data to output the HTML. This way, if you want to change the presentation of your list of items, you only need to edit the template, you don't need to touch the code at all.
Tuesday, February 06, 2007
It depends (Surprise).

No: Application code should generally not know about the presentation. If you embed html then you have coupled the model/application code with the view which is unnecessary. The presentation will have to know about the API of the model, but the model should not know about the presentation at all.

Yes: On the other hand you will soon - if you follow this principle - see that there is a lot of duplication of html and php snippets. These you should put into their own methods, but don't do this in your controller/php classes. Instead you should create some presentation or view specific classes that you can use.

So a clean and somewhat modualr solution consists of three parts:

An API which solves the application domain consisting of php classes.
A set of html+php pages which the user can interact with.
A set of methods that remove the worst duplication of html and inline php.

I hope this helps
Peter Monsson Send private email
Tuesday, February 06, 2007
If you're using an MVC framework, it should be in the V(iew) portion.  If you're not, having all the display stuff separate from the business logic is ideal.  Alternatively, if you create a "render" function, then you can still respect this distinction logically if not in files.
KC Send private email
Tuesday, February 06, 2007
Have a look at Smarty if your using PHP.
Tuesday, February 06, 2007
This is what I've been using: <>

It addresses something that's bothered me about template engines for a long time: that template engines separate HTML from PHP/whatever, whereas the real goal is to separate business logic from display logic.

Every other template system I've worked with has eventually grown either application-specific extensions or a callback mechanism to handle cases where displaying the user interface requires more sophisticated logic than is possible with the base template system. This one sidesteps that problem by allowing the templates to be written in PHP.
Wednesday, February 07, 2007
I think prado is a great framework:
Justin Silverton Send private email
Saturday, February 10, 2007

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz