Where to put reusable code? I have used Drupal 7 for a while, but am only starting to learn to use the tpl and template.php files. As you would expect, I have become good friends with the documentation, but am still having trouble putting all the pieces together at times.

One thing that I have not figured out how to do, but seems pretty basic, is to create a function in template.php or a custom tpl file (not sure what I need as well as how to do it) that outputs HTML. Let me explain what I mean... 

I have created a custom node--content-type.tpl.php. In there, I have put the following code in to replace the normal submitted by information:

&lt;?php if ($display_submitted): ?&gt;
  &lt;div class="submitted"&gt;
    &lt;div class="created"&gt;
      &lt;span&gt;Created by &lt;?php print $node-&gt;name; ?&gt; on &lt;?php print format_date($node-&gt;created, $type = 'medium'); ?&gt;.&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class="revised"&gt;
      &lt;span&gt;Last revised by &lt;?php print (user_load($node-&gt;revision_uid)-&gt;name); ?&gt; on &lt;?php print format_date($node-&gt;changed, $type = 'medium'); ?&gt;.&lt;/span&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;?php endif; ?&gt;


I am trying to figure out how to reuse this code in some of my other custom node tpl files without simply cutting and pasting it in. I am obviously missing something basic. How can I go about doing this?
Re-usable template code should ideally be in its own template, backed by its own theme function:

function THEME_theme() {
  return [
    'foo_partial' =&gt; [
      'variables' =&gt; ['node' =&gt; NULL],
    ],
  ];
}


foo-partial.tpl.php:

&lt;div class="submitted"&gt;
    &lt;div class="created"&gt;
      &lt;span&gt;Created by &lt;?php print $node-&gt;name; ?&gt; on &lt;?php print format_date($node-&gt;created, $type = 'medium'); ?&gt;.&lt;/span&gt;
    &lt;/div&gt;
    &lt;div class="revised"&gt;
      &lt;span&gt;Last revised by &lt;?php print (user_load($node-&gt;revision_uid)-&gt;name); ?&gt; on &lt;?php print format_date($node-&gt;changed, $type = 'medium'); ?&gt;.&lt;/span&gt;
    &lt;/div&gt;
  &lt;/div&gt;


There are a number of different permutations you could use to actually get that HTML into the node template, here's one of them:

function THEME_preprocess_node(&amp;$vars) {
  if ($vars['node']-&gt;type == 'page' &amp;&amp; $vars['display_submitted']) {
    $vars['submitted_html'] = [
      '#theme' =&gt; 'foo_partial',
      '#node' =&gt; $vars['node'],
    ];
  }
}


node.tpl.php

...
&lt;?php
if ($display_submitted &amp;&amp; $submitted_html):
  echo render($submitted_html);
endif;
?&gt;
...