Drupal 7 如何渲染页面?

发布于 2024-10-18 20:26:14 字数 777 浏览 1 评论 0原文

Drupal 7 如何渲染页面?它相当于 MVC 的视图系统。

当涉及到为请求渲染最终 HTML 页面时,我使用过的大多数 PHP 框架(基于 MVC)都采用一种方法,其中顶级布局/页面 PHP 文件设置基本文档结构,然后渲染我们通过包含或视图渲染方法的各种子视图。

//Simplified version
Page.phtml
    Head.phtml
    Body.phtml
        Banner.phtml
        Topnav.phtml
        Left.phtml
        Content.phtml
        Footer.phtml

我对 Drupal 对此的看法有点困惑。我正在阅读《Pro Drupal Development》,它以 page.tpl.php 开始于类似的领域。然而,它掩盖了主题引擎(这是正确的术语吗?)如何将 PHP 的各个部分放入此页面(不是批评,本书采用的方法与我所采用的方法不同)。

另外,Drupal 7 主题似乎没有 page.tpl.php 文件,因此(对我来说)不清楚页面骨架来自哪里。另外,从我读到的内容来看,似乎涉及“块”,但我不清楚“块”是否构成了整个页面,或者块是否是主题选择性使用的东西。

那么,从高级概念(或者按照您想要的详细程度)出发,Drupal 7 如何渲染页面?

我意识到您可以而且可能应该在不了解所有内容如何联系在一起的情况下开始使用 Drupal。我特别想了解各种 Drupal 系统如何组合在一起。向厌倦阅读此免责声明的人们致歉!

How does Drupal 7 render out a page? What's its equivalent to an MVC's view system.

When it comes to the rendering out the final HTML page for a request, most PHP frameworks (MVC based) I've worked with take an approach where a top-level layout/page PHP file sets out the basic document structure, and then renders our various sub-views via includes or view rendering methods.

//Simplified version
Page.phtml
    Head.phtml
    Body.phtml
        Banner.phtml
        Topnav.phtml
        Left.phtml
        Content.phtml
        Footer.phtml

I'm a little confused as to Drupal's take on this. I'm reading through Pro Drupal Development, and it starts off in similar territory with a page.tpl.php. However, it glosses over how the theme engine (is that the right term?) gets the various parts of PHP into this page (not a criticism, the book's taking an approach that different from the path I'm on).

Also, the Drupal 7 themes don't seem to have the page.tpl.php file, so its not clear (to me) where the page skeleton comes from. Also, from what I've read it seems like "Blocks" are involved, but it's not clear to me if "Blocks" makeup the entire page, or if blocks are something used selectively by themes.

So, working from high level concepts (or get as detailed as you'd like), how does Drupal 7 render out a page?

I realize you can, and probably should, start with Drupal without understand how everything ties together. I'm specifically trying to learn how the various Drupal systems come together. Apologies to people tired of reading this disclaimer!

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

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

发布评论

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

评论(3

随遇而安 2024-10-25 20:26:14

我认为这里有两个问题。首先,单个主题/模板如何渲染/使用/显示,其次,整个网站如何组合在一起。我认为第二个问题已经在上面得到了回答,所以我将尝试对第一个问题进行更多解释。

首先,一个模块(这就是 system.module 存在的原因,对于所有这些只有模块才能做的事情,比如实现 hook_menu())需要定义一个特定的主题函数/模板的存在,通过在 hook_theme()

说到这里,有可以使用两种不同的东西。主题函数,是以theme_为前缀的函数。通常用于具有更复杂逻辑/PHP 的页面较小元素,例如 theme_table()。还有一个模板,它是一个带有 tpl.php 的文件,例如 page.tpl.php

要使用主题函数/模板,您可以调用 theme() 像这样:

$output = theme('table', array('rows' => $rows, 'header' => $header));

或者你可以使用新的、所谓的可渲染数组。这基本上是一个数据+信息的数组,要使用哪个主题:

$output = array(
  '#theme' => 'table',
  '#rows' => $rows,
  '#header' => $header,
);

第二个是首选,因为这意味着它将尽可能晚地进行主题化,并且其他模块可以在钩子中更改它。结果将完全相同,drupal_render() 将在最终渲染期间调用 theme() 本身。

当调用 theme() 时,它会查找要使用的函数/文件,查看它是否已覆盖所使用的主题,是否存在所谓的模板建议,然后使用它们。

要覆盖主题中的主题函数,请将其复制到您的 template.php 文件并将“theme_”替换为“yourthemename_”,如果它是 tpl.php 文件,请将其复制到您的目录。

现在,最终渲染过程所做的基本上是构建一个大型 $page 数组,这是一个可渲染数组(一些文档位于 hook_page_alter() 然后调用 drupal_render() 就可以了。

页面/模板层次结构的全局结构(不是硬编码的,而是通过$page 中的任何内容)都是这样的:

html.php.tpl
  head.php.tpl
  page.php.tpl
    multiple regions
      multiple blocks

Drupal 7 中几乎所有内容都是一个块,包括实际内容,它通常是一个节点,主题定义它具有哪些区域,然后您可以在 UI 中为其分配块。

I think there are two questions here. First, how does a single theme/template get rendered/used/displayed and second, how does the whole site come together. I think the second question has already been answered above, so I'm going to try to explain the first a bit more.

First, a module (that's why system.module exists, for all these stuff that only a module can do like implementing hook_menu()) needs to define that a specific theme function/template exists, by declaring it in hook_theme()

Speaking of that, there are two different things which can be used. A theme function, which is a function prefixed with theme_. Often used for smaller elements of a page wich have more complex logic/PHP like theme_table(). And a template, which is a file with the tpl.php like page.tpl.php

To use a theme function/template, you can either call theme() like this:

$output = theme('table', array('rows' => $rows, 'header' => $header));

Or you can use the new, so called renderable array thing. That is basically an array of data + information which theme to use:

$output = array(
  '#theme' => 'table',
  '#rows' => $rows,
  '#header' => $header,
);

The second is preferred because it means that it will be themed as late as possible and other modules can change it in hooks. The result will be exactly the same, drupal_render() will then call theme() itself during the final rendering.

When theme() is called, it looks up the function/file to use, looks if it has been overriden the used theme, if there are so called template suggestions and then uses them.

To override a theme function in a theme, copy it to your template.php file and replace "theme_" with "yourthemename_", if it is a tpl.php file, copy it to your directory.

Now, what the final rendering process is doing is basically building up a large $page array, that is a renderable array (Some documentation of that is in hook_page_alter() and then call drupal_render() on it.

The global structure of the page/template hierarchy (which is not hardcoded, but given through whatever is in $page) is something like this:

html.php.tpl
  head.php.tpl
  page.php.tpl
    multiple regions
      multiple blocks

Almost everything is a block in Drupal 7, including the actual content, which is typically a node. A theme defines which regions it has and then you can assign blocks to it in the UI.

白芷 2024-10-25 20:26:14

当尝试了解模板如何组合在一起时,我发现的最好的工具是主题开发模块。它的工作原理有点像 Firebug,允许您单击页面的区域来查看用于页面各个部分的主题功能或模板文件,以及“建议" 可用于覆盖它们。

模板可以通过多种方式组合在一起。 Drupal 确实对它们的嵌套方式做出了一些假设,这些假设反映在模板文件中可用的默认变量中。但是,模板建议其中的可用变量可以修改。

据我了解,page.tpl .php 已替换为 html Drupal 7 中的 .tpl.php

我听过的对“块”的最好描述是,它们是您用来显示将在站点的各个区域中重复使用的内容的内容。最常见的用途是侧边栏,例如“最近更新的页面”列表或“谁现在在线”列表。

When trying to understand how the templates fit together, the best tool I've found is the Theme Developer module. It works a little like Firebug and allows you click on areas of a page to see the theme functions or template files that are used for various parts of the page, along with the "suggestions" that can be used for overriding them.

Templates can fit together in a variety of ways. Drupal does make some assumptions about how they'll be nested, which are reflected in the default variables that are available in the templates files. However, both the template suggestions and the available variables within them can be modified.

As I understand it, page.tpl.php has been replaced by html.tpl.php in Drupal 7.

The best description I've heard for "Blocks" is that they're what you use to display content that will be re-used in various areas site. The most common use is for sidebars, such as "Recently updated pages" lists or "Who is online now" lists.

魂牵梦绕锁你心扉 2024-10-25 20:26:14

尝试查看 drupalcon 上关于如何在 Drupal 7 上构建页面的演示,网址为 http://sf2010.drupal.org/conference/sessions/page-render-drill-down-drupal-7 特别是在开始 5 分钟后 (05:10)。抱歉不能在这里详细说明,因为我仍在自己观看。并尝试去理解它。 ;)

[更新]

观看演示后,我对 drupal 7 如何渲染页面进行快速总结:

而不是您在问题中提到的常见方法

其中顶级布局/页面 PHP 文件
列出基本文档结构,
然后渲染我们的各种子视图
通过包含或视图渲染方法

Drupal 通过像这样的呈现流方法呈现页面

  1. bootstrap
  2. menu_execute_active_handler
  3. page_callback
  4. Delivery_callback
  5. hook_page_alter()
  6. drupal_render($page)

但是,它掩盖了主题的方式
引擎(这个词正确吗?)得到
PHP 的各个部分都融入其中
页(不是批评,这本书的
采取不同于
我所在的路径)。

从渲染流程 3 (page_callback) 到流程 6 (drupal_render($page)) 开始提供各个部分,其中它开始返回 drupal 可渲染数组的数组,然后在您的主题中稍后可以使用 $page变量(由 drupal_render($page) 提供)来渲染 ie drupal 内容。

Try take a look a presentation on How pages are built on Drupal 7 from drupalcon at http://sf2010.drupal.org/conference/sessions/page-render-drill-down-drupal-7 specially after 5 minutes from start (05:10). Sorry can't specify much detail here cause i still watch it my self. And try to understand it as well. ;)

[Update]

After watch the presentation here is my quick conclusion on how drupal 7 render out a page:

Instead of common approach that you mention from your question

where a top-level layout/page PHP file
sets out the basic document structure,
and then renders our various sub-views
via includes or view rendering methods

Drupal render a page through a rendering flow approach like this

  1. bootstrap
  2. menu_execute_active_handler
  3. page_callback
  4. delivery_callback
  5. hook_page_alter()
  6. drupal_render($page)

However, it glosses over how the theme
engine (is that the right term?) gets
the various parts of PHP into this
page (not a criticism, the book's
taking an approach that different from
the path I'm on).

that various parts is served starting from rendering flow number 3 (page_callback) through flow number 6 (drupal_render($page)) where its start to return an array of drupal render-able array and then latter in your theme you can use the $page variable (served from drupal_render($page)) to render i.e drupal content.

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