doutuobao9736 2009-05-01 23:46
浏览 16

如何在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....?

  • 写回答

2条回答 默认 最新

  • dsqtl335227 2009-05-01 23:55
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100