Radical new idea for lazy rendering.

From: damz <damz@damz-dev.local>


---
 common.inc                      |   20 ++++++++++++++++++--
 theme.inc                       |    2 +-
 block/block.module              |    2 +-
 simpletest/simpletest.pages.inc |    2 +-
 4 files changed, 21 insertions(+), 5 deletions(-)

diff --git includes/common.inc includes/common.inc
index f787251..8be1099 100644
--- includes/common.inc
+++ includes/common.inc
@@ -3546,6 +3546,22 @@ function drupal_render_page($page) {
   return drupal_render($page);
 }
 
+class DrupalRenderableObject {
+  public $elements;
+
+  function __construct(&$elements) {
+    $this->elements = &$elements;
+  }
+
+  function __toString() {
+    return (string) _drupal_render($this->elements);
+  }
+}
+
+function drupal_render(&$elements) {
+  return new DrupalRenderableObject($elements);
+}
+
 /**
  * Renders HTML given a structured array tree.
  *
@@ -3586,7 +3602,7 @@ function drupal_render_page($page) {
  * @return
  *   The rendered HTML.
  */
-function drupal_render(&$elements) {
+function _drupal_render(&$elements) {
   // Early-return nothing if user does not have access.
   if (!isset($elements) || (isset($elements['#access']) && !$elements['#access'])) {
     return;
@@ -3702,7 +3718,7 @@ function drupal_render_children(&$element, $children_keys = NULL) {
   }
   $output = '';
   foreach ($children_keys as $key) {
-    $output .= drupal_render($element[$key]);
+    $output .= _drupal_render($element[$key]);
   }
   return $output;
 }
diff --git includes/theme.inc includes/theme.inc
index 5aa6114..9a25a58 100644
--- includes/theme.inc
+++ includes/theme.inc
@@ -1849,7 +1849,7 @@ function template_preprocess_page(&$variables) {
 
   // Render each region into top level variables.
   foreach (system_region_list($GLOBALS['theme']) as $region_key => $region_name) {
-    $variables[$region_key] = empty($variables['page'][$region_key]) ? '' : drupal_render($variables['page'][$region_key]);
+    $variables[$region_key] = empty($variables['page'][$region_key]) ? '' : _drupal_render($variables['page'][$region_key]);
   }
 
   // Add favicon.
diff --git modules/block/block.module modules/block/block.module
index 290a446..e914dde 100644
--- modules/block/block.module
+++ modules/block/block.module
@@ -784,7 +784,7 @@ function template_preprocess_block(&$variables) {
 
   if (is_array($variables['block']->content)) {
     // Render the block contents if it is not already rendered.
-    $variables['block']->content = drupal_render($variables['block']->content);
+    $variables['block']->content = _drupal_render($variables['block']->content);
   }
 
   $variables['template_files'][] = 'block-' . $variables['block']->region;
diff --git modules/simpletest/simpletest.pages.inc modules/simpletest/simpletest.pages.inc
index 7b3c642..43c3936 100644
--- modules/simpletest/simpletest.pages.inc
+++ modules/simpletest/simpletest.pages.inc
@@ -141,7 +141,7 @@ function theme_simpletest_test_table($table) {
       // Test name is used to determine what tests to run.
       $test['#name'] = $test_name;
 
-      $row[] = drupal_render($test);
+      $row[] = _drupal_render($test);
       $row[] = theme('indentation', 1) . '<label for="edit-' . $test_name . '">' . $title . '</label>';
       $row[] = '<div class="description">' . $description . '</div>';
 
