Introduction to Panel and Label

It may seem funny that we are "introducing" the Panel and Label controls now, especially since we have already been using them a lot throughout the examples.

On the surface, it may seem that Label is very simple -- you specify the Text that you want it to display and maybe some styles around it, and then you can just Render it out. And while Label and Panel controls should certainly be used for that purpose, they also offer a lot more in functionality.

Both the Label and Panel controls extend from the BlockControl class. The only difference between the two is that labels will render as a <span> and panels will render as a <div>.

In addition to defining the Text to display inside the control, these controls can also use a Template file to display contents, including child controls, or auto-render child controls. This offers a lot of power and flexibility, basically allowing you to render out an arbitrary number of dynamic controls inside of them.

The order of rendering for block controls are:

  • Display the Text (if defined).
  • Pull in the Template and render it (if defined).
  • If AutoRenderChildren is set to true, then get all child controls and call render() on all of them that have not been rendered yet, in the order they were added to the parent control.

In our example below, we define a Panel and assign textboxes as child controls. We specify a Text value and also setup a Template. Finally, we render that entire panel out (complete with the text, template and child controls) with a single render() call.

Note that even though 10 textboxes are being rendered, we never explicitly code a $objTextBox->render() call anywhere in our code. Instead, we let AutoRenderChildren do that for us.

Within the template file, the $this variable refers to the control being rendered.

Another type of block control to mention here is the Fieldset which draws a panel as an html fieldset, and has a legend. Otherwise, it is the same as a Panel.

Text Here Goes First

This is the Panel of Textboxes

Note how we can use $this in the template to refer to the control, like $this->Form->strMessage: "Hello, world!"

Fieldset Example