简体   繁体   中英

how to create complex container in php

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM