如何在不使用CakePHP中的“if()”语句的情况下保留多个类似的“视图/布局”文件DRY?

时间:2021-09-26 07:16:28

I'm developing a application with CakePHP, and I have two layouts: one for the home page, and another for the rest of the application. Except for some coding on the header, they're almost exactly the same. Is there something I can do to keep DRY, without using if () statements all over the layout?

我正在使用CakePHP开发一个应用程序,我有两个布局:一个用于主页,另一个用于应用程序的其余部分。除了标题上的一些编码,它们几乎完全相同。我可以做些什么来保持DRY,而不在整个布局中使用if()语句?

2 个解决方案

#1


I'd suggest using a ton of elements. This way, you can still keep all of the code in one place. For example, if this is your home page layout (boilerplate excluded):

我建议使用大量的元素。这样,您仍然可以将所有代码保存在一个位置。例如,如果这是您的主页布局(不包括样板):

<body>
     <?php echo $this->renderElement('pageHeader'); ?>
     <?php echo $this->renderElement('frontPageNotification'); ?>
     <?php echo $this->renderElement('navAndOtherStuff'); ?>
     ...
</body>

And this is your interior layout (where you want to display everything except for the frontPageNotification stuff:

这是你的内部布局(你想要显示除frontPageNotification东西之外的所有东西:

<body>
     <?php echo $this->renderElement('pageHeader'); ?>
     <?php echo $this->renderElement('navAndOtherStuff'); ?>
     ...
</body>

Now, if they're almost exactly alike, I would probably just use a single layout and have a few if statements within the layout itself to determine what's to be displayed. Also, you can choose which elements get displayed by looking at the $this->params array to figure out which controller and action is behind the page being loaded. Like:

现在,如果它们几乎完全相同,我可能只使用单个布局并在布局本身中有一些if语句来确定要显示的内容。此外,您可以通过查看$ this-> params数组来选择显示哪些元素,以确定正在加载的页面背后的控制器和操作。喜欢:

<body>
     <?php echo $this->renderElement('pageHeader'); ?>

     <?php if($this->params['controller'] == 'pages' && $this->params['action'] == 'index') { echo $this->renderElement('frontPageNotification'); } ?>
     <?php echo $this->renderElement('navAndOtherStuff'); ?>
     ...
</body>

Which is, admittedly, rather ugly. Just trying to present all the options I could think of :)

诚然,这是相当丑陋的。试着提出我能想到的所有选项:)

Good luck

#2


Define an interface to a layout type. Every place you have an "if layout main" or "if layout other" define an interface function

定义布局类型的接口。您拥有“if layout main”或“if layout other”的每个地方都定义了一个接口函数

interface IMyLayout
{
    function DrawArea1(...)
    ...
}

class CMyMainLayout implements IMyLayout
{
    function DrawArea1()
    {
        //... draw area 1 for main
    }
    ...
}


class CMyOtherLayout implements IMyLayout
{
    function DrawArea1()
    {
        //... draw area 1 for other
    }
}

Then you just select one or the other by newing the correct object

然后,您只需通过新建正确的对象来选择其中一个

 if ($main)
 {
     $layout = new CMyMainLayout;
 }
 else
 {
     $layout = new CMyOtherLayout;
 }

 $layout->DrawArea1();

#1


I'd suggest using a ton of elements. This way, you can still keep all of the code in one place. For example, if this is your home page layout (boilerplate excluded):

我建议使用大量的元素。这样,您仍然可以将所有代码保存在一个位置。例如,如果这是您的主页布局(不包括样板):

<body>
     <?php echo $this->renderElement('pageHeader'); ?>
     <?php echo $this->renderElement('frontPageNotification'); ?>
     <?php echo $this->renderElement('navAndOtherStuff'); ?>
     ...
</body>

And this is your interior layout (where you want to display everything except for the frontPageNotification stuff:

这是你的内部布局(你想要显示除frontPageNotification东西之外的所有东西:

<body>
     <?php echo $this->renderElement('pageHeader'); ?>
     <?php echo $this->renderElement('navAndOtherStuff'); ?>
     ...
</body>

Now, if they're almost exactly alike, I would probably just use a single layout and have a few if statements within the layout itself to determine what's to be displayed. Also, you can choose which elements get displayed by looking at the $this->params array to figure out which controller and action is behind the page being loaded. Like:

现在,如果它们几乎完全相同,我可能只使用单个布局并在布局本身中有一些if语句来确定要显示的内容。此外,您可以通过查看$ this-> params数组来选择显示哪些元素,以确定正在加载的页面背后的控制器和操作。喜欢:

<body>
     <?php echo $this->renderElement('pageHeader'); ?>

     <?php if($this->params['controller'] == 'pages' && $this->params['action'] == 'index') { echo $this->renderElement('frontPageNotification'); } ?>
     <?php echo $this->renderElement('navAndOtherStuff'); ?>
     ...
</body>

Which is, admittedly, rather ugly. Just trying to present all the options I could think of :)

诚然,这是相当丑陋的。试着提出我能想到的所有选项:)

Good luck

#2


Define an interface to a layout type. Every place you have an "if layout main" or "if layout other" define an interface function

定义布局类型的接口。您拥有“if layout main”或“if layout other”的每个地方都定义了一个接口函数

interface IMyLayout
{
    function DrawArea1(...)
    ...
}

class CMyMainLayout implements IMyLayout
{
    function DrawArea1()
    {
        //... draw area 1 for main
    }
    ...
}


class CMyOtherLayout implements IMyLayout
{
    function DrawArea1()
    {
        //... draw area 1 for other
    }
}

Then you just select one or the other by newing the correct object

然后,您只需通过新建正确的对象来选择其中一个

 if ($main)
 {
     $layout = new CMyMainLayout;
 }
 else
 {
     $layout = new CMyOtherLayout;
 }

 $layout->DrawArea1();