tags:

views:

98

answers:

2

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....?

A: 

Sounds like you are re-creating divs. I think it would be better if you defined a format of divs, and then created different css styles to style each set of divs differently.

so it would be something like:

<div class="title">
    <div class="left">my left content</div>
    <div class="right search">my right search content</div>
</div>

I'd keep the complex logic for the css rules, and then make a seperate template for each of the types that you have.

Jon Snyder
Either I don't get you, or you don't get me. :) It's not a question of how to style the different bar types, obviously that's going to be done with css. It's a question of whether or not that content exists at all in a given panel. (Some of the bar-types might have default content in them, etc., I just simplified for my example above.) Having 7 different template files, all of which are pretty similar, but which might have slight differences doesn't seem like a good idea to me, when I can use code to create those differences all in one place.
sprugman
A: 

You should consider using the Decorator Pattern. The scenario given as an example at Wikipedia seems to be the very same problem presented by you.

Petrunov