How Does Drupal 7 Render Out a Page?

Alan Storm picture Alan Storm · Feb 22, 2011 · Viewed 13.6k times · Source

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!

Answer

Berdir picture Berdir · Feb 22, 2011

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.