When to use Helpers

Should I always use helpers in my templates or when is the best situation to use helpers?

Not sure what you see as the alternative. If you mean “should I generate all the HTML in my templates with helpers”, then almost certainly no; I use them for generating all links and img tags and some tables and other things, but I also have lots of raw HTML embedded. If, on the other hand, you mean “should I only use helpers in templates, or are there other good places to use them”, then the answer is yes, helpers should only be used in templates.

Thank you. Now I think I have it clear in my mind. I was unsure if I should generate the entire code using helpers or just more strategic tags like img, links and so on.

Apart from img and css and link tags, I don’t really have any set rule that I follow. Whatever makes the code clearer. Sometimes a list is most manageable by building an array and using $this->Html->nestedList, sometimes it’s better to use a bunch of $this->Html->tag('li', ...) calls, sometimes the best way is <li><?= ... ?></li>.

I’m in the whatever-makes-the-code-simpler camp also.

I’m fond of writing helpers that take an entity as the argument and give me a nice explanatory name.

// in a template

echo $this->Html->tag('h2', $author->getName();
foreach ($articles as $article) {

I have a lot trouble visually parsing complex code so being able to ‘read’ my code as much as possible like a sentence is a big help.

As an added benefit, I will have a single place to go look for all those pesky links when I realize they don’t have the attributes I want. In this way I view them as a scaled down version of an Element.

In fact helpers are a very interesting feature which I’m getting familiar with. I’m playing with it and I would like to run few tests about the performance. Since helpers are server language code (PHP) making the output of HTML I’m just curious to understand if the performance is affected somehow.

I came from pure PHP and I always been told to avoid writing PHP together with HTML and now I have this change of paradigm which makes it possible to generate html code using php.

I have been experiencing different concepts and template languages. As a matter of fact the more I study I get the conclusion that PHP can be used as a template language as long as the heavy PHP code is written in the model and controller.

What I have to understand now is about the performance and to compare:

Template with many helpers X Template with few helpers

Helpers X Twig

If anyone has already tested it I would appreciate your feedback.

Frameworks like CakePHP are built to let you build good code quickly, and in a maintainable way. That pretty much works against performance. If you’re going to be building a site that needs to reliably serve thousands of requests per second, then no framework will likely be useful, and PHP may not be the right language at all. If you’re building sites that handle thousands of requests per hour, then the difference between a request taking 0.2s and even 1.5s to run is arguably pretty meaningless, and you should be much more concerned with functionality and user experience than performance.

That’s a good and valid point.
Thousands of requests per second goes to my future tests with Swoole and Phalcon and maybe PHP 8 using git.
I think PHP has been evolved pretty much and it has became a very powerful language.
But I have to say CakePHP is my favorite framework so far.