I'm creating a container type which has a lot of possible variations on what can be included:
- title bar
- search bar
- form bar
- tab bar
- custom bar
- info bar
- action/status bar
- content area
Of these, only the content area is required. There are sub-types that would share common sets of bars, but they can also be mixed and matched in arbitrary combinations. Also, each bar-type can have content on either the left or the right side.
I've been going down the path of creating a Panel class with properties like:
- info_left
- info_right
- search_left
- search_right
- etc.
then doing this kind of logic:
// snip from __toString()
if ($this->info_left || $this->info_right) {
$result .= $this->add_bar('info', $this->info_left, $this->info_right);
}
if ($this->search_left || $this->search_right) {
$result .= $this->add_bar('search', $this->search_left, $this->search_right);
}
// end snip
private function add_bar($type, $left, $right) {
$result = <<< HERE
<div class="$type">
<div class="left">$left</div>
<div class="right">$right</div>
</div>
HERE;
}
and to instantiate a Panel:
$p = new Panel("my panel");
$p->info_left = "my left content";
$p->search_right = "my right search content";
echo $p;
So far, I've stayed away from using a template engine because the logic seems complex enough that I'd almost rather keep it in the class. (The class has very little business logic, but lots of presentation logic.) Maybe there's a better approach....?