How to wrap Drupal "$messages" in a div? How can I wrap $messages in a div using template.php file?

This is my code:

function THEME_status_messages($variables) {
  $display = $variables['display'];
  $output = '';

  $status_heading = array(
    'status' =&gt; t('Status message'),
    'error' =&gt; t('Error message'),
    'warning' =&gt; t('Warning message'),
  );
  foreach (drupal_get_messages($display) as $type =&gt; $messages) {
    $output .= "&lt;div class=\"message $type\"&gt;&lt;a class=\"message-close\"&gt;&lt;/a&gt;\n";
    if (!empty($status_heading[$type])) {
      $output .= '&lt;h2 class="element-invisible"&gt;' . $status_heading[$type] . "&lt;/h2&gt;\n";
    }
    if (count($messages) &gt; 1) {
      $output .= "&lt;ul&gt;\n";
      foreach ($messages as $message) {
      $output .= '&lt;li&gt;' . $message . "&lt;/li&gt;\n";
    }
    $output .= "&lt;/ul&gt;\n";
    }
    else {
      $output .= reset($messages);
    }
    $output .= "&lt;/div&gt;\n";
    }
    return $output;
}


I need to wrap so the result it's this:

&lt;div id="messages"&gt;
  &lt;div class="message warning"&gt;
    &lt;a class="message-close"&gt;&lt;/a&gt;
  &lt;/div&gt;
  &lt;div class="message error"&gt;
    &lt;a class="message-close"&gt;&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;


I'm not interested to in doing it in page.tpl.php like this because it will leave an empty div if there's no messages:

&lt;div id="messages"&gt;&lt;?php print render($page['messages']); ?&gt;&lt;/div&gt;


So far I wasn't able to output the #messages div, every time I try something I break everything or nothing happens.

Thank you.
Change this:

return $output;


To this:

return '&lt;div id="messages"&gt;' . $output . '&lt;/div&gt;';