PHP: Separate business logic and presentation logic, is it worth it?

Possible duplicate:
Why should I use a template system in PHP?

I was just wondering how many developers actually do this?

So far I have not done this, and I was just curious if this really helps make things cleaner and easier to follow. I heard using template engines like Smarty, I heard the opposite. The fact that they simply create unnecessary overhead and, in fact, like to learn a new language.

Does anyone have experience with templates? What are your feelings for them? Are big projects useful or just a waste of time?

On the side of the note: the company I work for does not have a designer, in this project there are only two developers accused of redesigning / updating. I also use a little AJAX, will this have problems with the template engine?

+4
source share
7 answers

This practice not only makes the code look clean, but also has many long-term and short-term benefits.

You cannot make a mistake in organizing the code. First, it simplifies maintenance and makes it easier to read if someone else should pick you up after you. I have worked with Smarty before, and this is good, it makes designers work from interfering with the program code.

Using template systems and frameworks will simplify the task. There is a right rule that you can follow that is DRY (do not repeat it yourself). A framework will help you achieve this goal.

You may want to look into MVC , this is the model this framework is based on. But you can implement this design without the need for a framework. Avoid the learning curve. For frameworks such as Zend , the learning curve is much larger than some others.

I found that Code Igniter is pretty easy to use and they have some VERY useful video tutorials on their website.

Best of luck !!

+10
source

This is actually business logic that needs to be separated from representations. You can use php as a "template language" inside views.

You can use ajax for any template engine, I think.

Edit

My initial answer addressed the issue of whether to use the template engine or not to generate your html.

I argued that php is good enough for template tasks if you separate business logic from presentation logic.

It is worth doing even for simple pages, because it allows you to:

  • isolate the code that is the brain of your application from the code that is the face, and therefore you can change the face without delving into the brain, or you can improve the brain without slowing down the look.
  • isolate 80% of errors in 20% of your code
  • Create reusable components: you can assign a different presentation code to the same business code and vice versa;
  • separate problems of requesting functions (business code) from problems of design requests (presentation code), which are also usually associated with different people on the client side, and different people on the contractor side.
  • use different people to write business code and presentation code; you can force the designer to directly process the presentation code with minimal knopedge php;

A simple solution that mimics MVC and does not use objects can be:

  • use a single php controller file that receives all requests through a .httpdaccess file;
  • the controller decides which business and presentation code to use, depending on the request
  • the controller then uses the include statement to include the php business file
  • business code makes it magical and then includes a php presentation file.
+6
source

PHP is a template engine (or, if you want, a hypertext preprocessor). When HTML mixes heavily with PHP logic, it becomes very difficult to maintain, so you will have functions defined separately to create different parts and just create a page from short calls to functions built into HTML. This is done so, I do not see much difference between Smarty and raw PHP, except for the choice of delimiters.

+2
source

Sharing problems is a very important tenant for any type of software development, even on the Internet. Too many times I have found that people simply drop everything into as few files as possible and call it day. This is definitely the wrong way to do this. As already mentioned, this will help with code support for others, but more than that, it will help you read the code. When everything is separated, you can easily think.

The Ignitor code, I found, was the easiest to learn the PHP framework. To a large extent, I began my current work and worked with it for several days, never heard of it in order to use it quite effectively. I do not see another language in this. Basically, using a structure makes me organize things in a controlled way, and the added functionality is mandatory for using plugins, etc. For jQuery or for importing packages in Java. The idea that he, like learning another language, seems almost silly.

So, in a word, organize to organize an organization. Keep in mind, however, that there is a level of abstraction that just becomes absurd. The rule of thumb is that the class (or file in our case) should do something very well. This does not mean that it is a class that wraps printing, but takes a string, formats it using a complex algorithm, and then prints it (this is just an example). Each class must do something specific, and you can do it without any frameworks. What makes MVC great, though, is that it allows you to organize things further, not only at the level of one class, but also at the level of the “packages” that are Model, View and Controller (at least in the case of these frameworks; there are other ways of batch projects). So, now you have separate classes that work well, and then you group them with similar classes that do other things well. Thus, everything is supported very cleanly managed.

The last level to think about when you have things organized into classes and then packages is how these classes gain access between packages. When using MVC, access will usually pass through Model ↔ Controller ↔ View, thereby separating the model (usually this is database material and business code in the PHP world) from the view (which usually receives information from the user and passes it along with the controller, which then will receive additional information from the model, if necessary, or will do something else with the input information). A controller type acts like a switch between two other packets normally . Again, there are other ways to go with packaging, etc., but this is the usual way.

I hope this helps.

+2
source

Smarty and other php frameworks really do nothing but compile for PHP, and in most cases they cache their results to provide faster processing. You can do it all yourself, but if you ever look at the compiled templates that Smarty generates and compare with the original Smarty template that you create, you can see that it is much more readable than the other.

0
source

I write mostly mod_perl these days and started using templates (HTML :: Template) halfway through our current project. If I had to make a decision again, I would use templates from the very beginning - rewriting later to use templates is tedious, although useful, because you get cleaner and cleaner code. For something bigger than 2-3 pages in php, I would also use some template engine.

0
source

One of the big advantages of a templating engine, such as Smarty, is that non-developers can use it to embed the necessary logic that is used in the interface (in fact, it is impossible to separate the logic and display on all but the simplest sites). However, if the developer is the one that supports the pages, then using PHP would be preferable, in my opinion.

If you allocate large logic blocks and maintain a consistent patten for loops and for each flow control statement (i.e. do not use print statements or use only print instructions for single lines, etc.). Then it should be all right.

0
source

All Articles