Module to do an action based on content type user sees
Assuming you're using Drupal 7 you can do all of this in hook_node_view(), e.g.

function MYMODULE_node_view($node, $view_mode, $langcode) {
  // Check the node type and that the user is viewing a full node page
  if ($node-&gt;type == 'article' &amp;&amp; $view_mode == 'full') { 
    // Get some field values from the node
    $field_items = field_get_items('node', $node, 'field_name');

    // Get the first field value.
    $first_field_item = array_shift($field_items);

    // The index will depend on the type of field; for taxonomy term refs it's 'tid'
    $value = $first_field_item['tid']; 

    // Perform some complex operation on the value.
    $new_value = MYMODULE_complex_op($value);

    // Add the new value to the node content, to be found in the $content array
    // in node--article.tpl.php
    $node-&gt;content['my_new_value'] = array(
      '#markup' =&gt; $new_value
    );
  }
}


In your template file you'll be able to call print render($content['my_new_value']); to output the variable to the page.

Obviously that'll need some adjusting for your own needs but hopefully it'll help.