How do I create a variable that my page.tpl.php file can access? In Drupal 7, I have a module that implements hook_node_load. It creates a variable that I'd like to make use of in my page.tpl.php file. Let's pretend it is the count of nodes that are of the same type as the currently loaded node. As of now, I'm just making the variable global, like so:

global $node_count;
$node_count = db_query("SELECT count FROM {node} WHERE type = :type", array(":type" =&gt; $node-&gt;type"))-&gt;fetchAll()[0]-&gt;count;


Then, within my page.tpl.php file, I have to do something like the following:

global $node_count;
echo "&lt;p&gt;$node_count&lt;/p&gt;";


I know that there is a way to make my $node_count variable in scope, in the same way that $node or $user are. The tutorials I find on the subject seem to involve rewriting the entire theme function - I'm not trying to do anything that complicated. Is there a simple way to just make this variable in scope in my .tpl.php files? Something like a function?
In Drupal theming, template preprocess functions are the functions that prepare the variables that can be used in a certain template. Template preprocess functions are executed one after another and they pass around an array with all variables. You can add your own in your theme's template.php, in the form of function THEMENAME_preprocess_TEMPLATENAME(&amp;$variables) { // Your code here. }.

If you're adding a value to a node object in a hook_node_load implementation, that probably looks something like this:

function mymodule_node_load($nodes, $types) {
  foreach ($nodes as $node) {
    $node-&gt;myvariable = 'somevalue';
  }
}


Now you want to access $node->myvariable in your page.tpl.php. For this, you don't even need to write a preprocess function, because Drupal already adds the current node object to the variables by default. (This happens in template_prepocess_page.) This means that you can simply add this to your page.tpl.php:

&lt;?php if ($node-&gt;myvariable): ?&gt;
  &lt;div class="myvariable"&gt;
    &lt;?php print $node-&gt;myvariable; ?&gt;
  &lt;/div&gt;
&lt;?php endif; ?&gt;