4

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.

TIA

Scott Mc
  • 143
  • 3

2 Answers2

2

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.

Elements: http://book.cakephp.org/3.0/en/views.html#elements

Cells: http://book.cakephp.org/3.0/en/views/cells.html

Stephan
  • 121
  • 5
1

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!

NexusRex
  • 136
  • 3