为首页使用单独的模板文件是否更好?

发布于 2024-08-11 14:06:35 字数 556 浏览 2 评论 0原文

就以下方面而言:

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

以下两种方法中哪一种更好?

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

方法 1

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

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

方法 2

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

In terms of:

  1. Speed
  2. Required processing (which will influence speed)
  3. Following standards

Which of the following two methods will be better?

I want to create a general page layout, however, the frontpage will look different from the normal look and feel.

Method 1

Creating a normal page.tpl.php file but with the following code in it:

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

Method 2

Creat two distinct pages, namely page.tpl.php and front.tpl.php. Code will be duplicated, but the frontpage and other pages will each have their own dedicated file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狂之美人 2024-08-18 14:06:35

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

不过我有兴趣看看主页的具体代码是什么。 Drupal 将为首页提供一个“front”CSS 类,以便可以对其进行不同的样式设置,并且可以创建仅在首页上显示的块。因此可能不需要特定的首页模板。

I would say that method 2 is better. I think speed will not be greatly affected either way, and there are no strict standards about this, but excessive branching in template files is discouraged.

However I would be interested to see what the homepage specific code is. Drupal will give the front page a "front" css class so it can be styled differently and blocks can be created to display only on the front page. So there may not be a need for a specific front page template.

信愁 2024-08-18 14:06:35

我将以相反的顺序解决您的观点:

标准

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

所需处理

我不认为这两种不同方法的本质存在明显的差异。在其他一切都一样的情况下,计算机打开一个文件(模板)并处理其中的 PHP 仍然是一回事,无论这是同一个文件还是另一个文件。解决方案 #1 多了一个 if 语句,但是......它真的有区别吗?

SPEED

如果您确实急需优化,我会阅读(如果我没记错的话Pro Drupal Development)认为通过模板对页面进行主题化比通过函数进行主题化要慢 5 倍,因此您也可以考虑该解决方案,尽管这只会带来好处我相信主页不可缓存。

哈!

I will tackle your points in reverse order:

STANDARDS

I believe the standard accepted method (at least when working with zen-based themes) is to create a specific template. You actually do not need to put any additional theming function for it to work, as if you name it page-front.tpl.php it will be used exclusively for the front page. This of course applies only if you really need a separate template (see Jeremy's answer about this).

REQUIRED PROCESSING

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文