Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

While implementing a flash game portal I decided to "component-ize" my views. The repeating elements were a no brainer and the templates for these were placed in elements. I pass through data from the controller to the element via the view using the Html helper.

This is all working fine, and except for a very minor subset of elements, I haven't had too many issues with complicated logic in the view in order to massage parameters - in other words they have all been completely pass-through and therefore very transparent to the elements.

Here-in lies my problem. I realize I have over 20 elements which far eclipses my paltry 8-10 views and 3 controllers. I've also had the urge to turn almost everything into a "component" and convert it into an element. For example, I have a rating control that is used on only one page - the game play page - and I recently contemplated converting it to an element. I'm wondering if I've gone overboard at this point.

So, in summary, my question boils down to what are the best practices when using the CakePHP Html helper and what makes a chunk of html a candidate for an element (aside from the the obvious of only writing it once). I've read the cakePHP manual and it gives "rough" guidelines but I'm looking for some "field level" or real world experience to guild my decisions.


share|improve this question
up vote 1 down vote accepted

I think you are right on with taking it down to the micro-level, even with an element that is only used on a single page. It's a great way to keep everything very compartmentalized.

I use CodeIgniter predominantly but have found the same practice to be very beneficial. Imagine that you want to modify your rating control... you just duplicate the element, rename, modify, and reference the new element.

It's just very clean!

share|improve this answer

You're on the right track. Typically you will want to use an Element either to cleanup a code and reduce the amount of HTML or to create a re-usable code segment.

I will assume you are using CakePHP3, which also has View Cells. View Cells are essentially an Element with a mini-controller. This will help also reduce the code within your parent controllers and allow the Cells to be reused as well.

In my experience, if there is no logic required an Element is the way to go, when logic is required, I use Cells.

You can learn more about the difference in the CakePHP manual. I have included two references, one to Elements and one to Cells.



share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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