0

按照:

  1. 速度
  2. 所需处理(会影响速度)
  3. 遵循标准

以下两种方法哪个更好?

我想创建一个通用的页面布局,但是,首页看起来与正常的外观不同。

方法一

创建一个普通的 page.tpl.php 文件,但其中包含以下代码:

.....
<body>
  <?php if (isFront()) { 
          // lots of stuff for the frontpage
        }
        else 
        {
          // lots of stuff for the other pages
        }
  ?>
</body>

方法二

创建两个不同的页面,即 page.tpl.php 和 front.tpl.php。代码将被复制,但首页和其他页面将各自拥有自己的专用文件。

4

2 回答 2

3

我会说方法2更好。我认为无论哪种方式速度都不会受到太大影响,并且对此没有严格的标准,但不鼓励在模板文件中过度分支。

但是我很想看看主页特定的代码是什么。Drupal 会给首页一个“front” css 类,因此它可以有不同的样式,并且可以创建块以仅在首页上显示。因此,可能不需要特定的首页模板。

于 2009-11-17T06:24:15.060 回答
2

我将按相反的顺序处理您的观点:

标准

我相信标准的接受方法(至少在使用基于 zen 的主题时)是创建一个特定的模板。您实际上不需要添加任何额外的主题功能来使其工作,就好像您将其命名为page-front.tpl.php将专门用于首页一样。这当然仅适用于您确实需要单独的模板(请参阅 Jeremy 对此的回答)。

所需处理

I don't think there is a perceivable difference intimately connected with the nature of the two different methods. Everything else being the same, it's still a matter for the computer to open a file (the template) and process the PHP in it, whether this is the same file or another one. Solution #1 has an if statement more but... does it really make a difference?

SPEED

If you are really in bad need for optimisation, I read (if I am not wrong on Pro Drupal Development) that theming a page via a template is 5x times slower than doing it via a function, so you might consider that solution too, although this would only bring a benefit if the homepage is not cacheable, I believe.

HTH!

于 2009-11-17T16:01:55.727 回答