Using hook_theme() when there is a preprocess
So, hook_theme is where you define your theme implementations and templates to use for them. In your case, that would look something like:

foo_alerts_theme() {
  $module_path = drupal_get_path('module', 'foo_alerts');
  return array(
    'alerts_theme_name' =&gt; array(
      'path' =&gt; $module_path . '/templates',
      'template' =&gt; 'alerts-theme-template-name',
      'variables' =&gt; array(
        'node' =&gt; NULL,
      );
    ),
  );
}


Your template file, named alerts-theme-template-name.tpl.php goes in the templates folder in your foo_alerts module folder. In your page preprocess you're going to pass in the node loaded from the alerts function to the theme function, so that would look like this:

function foo_alerts_preprocess_page(&amp;$vars) {
  $vars['alerts'] = '';

  if ($nid = foo_alerts_check_alerts()) {
    $node = node_load($nid);

    if (isset($_COOKIE['alert'.$node-&gt;nid]) &amp;&amp; $_COOKIE['alert'.$node-&gt;nid] == 'closed') {
      return;
    }

    $vars['alerts'] = theme('alerts_theme_name', array('node' =&gt; $node));
  }
}


That leaves all of the non-template logic still in the preprocess function, and returns the output of theme(), which will be the html from your template. Your template would then be something like this:

&lt;div id="alert-bar" rel="&lt;?php print $node-&gt;nid; ?&gt;"&gt;
  &lt;div class="inner"&gt;
    &lt;h2&gt;News Alerts:&lt;/h2&gt;
    &lt;?php if (isset($node-&gt;field_link['und'][0]['url'])) : ?&gt;
      &lt;?php print l($node-&gt;title, $node-&gt;field_link['und'][0]['url'], array(
        'attributes' =&gt; array(
          'title' -&gt; $node-&gt;field_link['und'][0]['title'],
          'class' =&gt; array('alert-link'),
        ),
      )); ?&gt;
    &lt;?php else : ?&gt;
      &lt;span class="alert-link"&gt;&lt;?php print $node-&gt;title; ?&gt;&lt;/span&gt;
    &lt;?php endif; ?&gt;
    &lt;a href="#" id="close-alert-bar"&gt;&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;


Does that all make sense? Do you see how the code the old dev team left you with got turned into this? If you have any questions, let me know and I'll try to explain better what I did.