How not to mix presentation with logic?

I use PHP quite a bit, and whenever I see a thread of “hatred of PHP” in some forum or even popular discussions related to PHP, I usually see something like:

PHP is too dirty / sloppy / crappy because you have a confusing network of views and logic.

Then I see that PHP-Defenders will respond to some version of the above that this is not necessarily true. I thought: "How is this possible ...?" and "What is considered a mix of presentation with logic?" I could not understand, so now I am here.

Which one is best practice? Or is there an even better way that I don't know about?

<?php if(!function()) { echo '<div id="results">The result is a lie.</div>'; } ?> 

or

 <div id="results"> <?php if(!function()) { echo 'The result is a lie.'; } ?> </div> 

I know that the above examples do not really look like big deals, but after looking at my web applications I realized that all this is dirty (because I was mixing HTML and PHP), so I was hoping it was a good way to develop, keeping while all is good and neat.

+6
php
source share
7 answers

You might want to look at a template engine like smarty. This may separate him from you.

Communication: http://www.smarty.net/

Some (hopefully balanced) additions see a rather large comment section below:

There seems to be a lot of discussion about smarty. As far as I know, there are two additional camps in this,

one says: Template engine, fine, but smarty is not the best; (enter your selection here). e.g. twig or dwoo.

and the other says: do not use a separate template engine! Because PHP is quite capable of doing this on its own. Examples from comments:

Use Zend_View and Savant

+11
source share

Think about logic and presentation, where logic is a type of electrical outlet and presentation is a light bulb. You may now have several sockets in your home, some with different sizes. You can also have a bunch of light bulbs in different colors, some cheap, some expensive. The fact is that one lamp can go into one or several socks, and one socket can accept more than one lamp, but you can only match one lamp in one socket at a time.

So, if you have a really good light bulb (or a really good html template), you want to move this light bulb to where you need it (or apply the same logic). And if one day you decide that you want to use the blue light, you can just change the lamp, you do not need to install a completely new electrical connector to change the color.

Returning to the logic and presentation, if you have a general case when you have a form on a web page, sometimes you show this form when the user first loads the page, and sometimes you want to show it after the user has filled in some of the inputs, but maybe , there were errors or missing information. In this case, the logic will not do anything, just show the form or try to process the form, find the error and then display the form that reveals the errors.

You can do this with mixed logic and presentation in the same file, of course, but what happens when several people start editing your script? Maybe the script breaks down and they don’t know what to do, so they comment on some important section to make it work again. It is like someone is about to change a light bulb, and then decides to reset your light switches. But sometimes, when you encounter poor wiring, there is no other way to fix the problem, the problem comes from a simple "Please change the light bulb" to "Make the light work." In a properly designed system where components are isolated in a reasonable way, it is usually easier to fix.

In other words, the mixing logic and presentation are similar to wiring your bulbs using a bare wire, and then connecting the wires to the network without using a circuit breaker.

+2
source share

The second example is better.

The easiest way to avoid confusing presentation with logic is to use an MVC framework like Symfony or CakePHP. They allow you to separate data (models) with logic (controller) and presentation (s).

Another answer is good: use templates. Templating is almost always part of the MVC framework.

+1
source share

You can define helper methods in the included file or at the top of the page:

 <?php function result_if_good() { if (!function()) { return 'The result is a lie.'; } } ?> 

And then elsewhere, in your HTML:

 <div id="results"><?php echo result_if_good();?></div> 

Saving all your logic from the presentation.

0
source share

If you need a true separation between your logic and your interface, you should check out the MVC pattern .

I use the views in the MVC template with the template engine to get clean HTML files for my views

0
source share

I think you can try using ZendFramework .

0
source share

I prefer a situation where the script highlights HTML and javascript, if necessary. In other words, something more similar (in pseudo python):

 #Primitives def htmlheader(title): return "<html><head><title>%title</title></head>" % title def body: return "<body>" def para(content): return "<p>%content</p>" % content def htmlend: return "</body></html>" #New file: View print htmlheader() print body() print para("This is my paragraph") print para("Hello, %salatation %lastname" % dbresult.salutation, dbresult.lastname) print htmlend() 

PHP suffers from being surrounded by <>, which are already quite difficult to read.

Ymmv

Using the primitives to release this actual html, you can create a controller layer that simply passes the clean data to the view. More pseudo-Python:

 #ViewController db = dbconnect("host", "user", "password"); view.mainbody.show(getCurrentNews(db)) view.header.show(getTopHeadLines(db)) if (user.isLoggedIn): view.footer.userNavigation() else: view.footer.showDefault() view.render 
0
source share

All Articles