Index: includes/theme.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/theme.inc,v
retrieving revision 1.624
diff -u -p -r1.624 theme.inc
--- includes/theme.inc	24 Nov 2010 03:30:59 -0000	1.624
+++ includes/theme.inc	27 Nov 2010 02:05:06 -0000
@@ -2256,7 +2256,6 @@ function template_preprocess_page(&$vari
   $variables['language']          = $GLOBALS['language'];
   $variables['language']->dir     = $GLOBALS['language']->direction ? 'rtl' : 'ltr';
   $variables['logo']              = theme_get_setting('logo');
-  $variables['messages']          = $variables['show_messages'] ? theme('status_messages') : '';
   $variables['main_menu']         = theme_get_setting('toggle_main_menu') ? menu_main_menu() : array();
   $variables['secondary_menu']    = theme_get_setting('toggle_secondary_menu') ? menu_secondary_menu() : array();
   $variables['action_links']      = menu_local_actions();
@@ -2294,6 +2293,15 @@ function template_process_page(&$variabl
   if (!isset($variables['title'])) {
     $variables['title'] = drupal_get_title();
   }
+
+  // Generate messages last in order to capture as many as possible messages for
+  // the current page. Since render($content) and other commands in page.tpl.php
+  // can still trigger messages, only set 'messages' if it has not been set yet,
+  // so other modules are able to come up with more advanced ESI/string
+  // replacement solutions.
+  if (!isset($variables['messages'])) {
+    $variables['messages'] = $variables['show_messages'] ? theme('status_messages') : '';
+  }
 }
 
 /**
