The standard method to set up variables and pass them to a template file is through the use of a preprocess function. In the case of the node.tpl.php file this would look something like:
function MYTHEME_preprocess_node(&$vars) {
$field_1_items = field_get_items('node', $node, 'field_something');
$field_2_items = field_get_items('node', $node, 'field_something_else');
$computed_value = $field_1_items[0]['value'] + $field_2_items[0]['value'];
$vars['computed_value'] = $computed_value;
}
Then in your template file you could do something like:
<div class="some-class">
<?php echo $computed_value; ?>
</div>
That's just an example, your use case will likely differ significantly; but that's the basic method behind it.
Remember that the template files should only ever be used for presentation (adding HTML markup and outputting PHP variables that have been prepared elsewhere), never for logic. The only exception to this is conditional logic (if
/switch
statements) and other basic logical operations such as &&
, ||
, etc. As you're performing a calculation on field results you're implementing logic, so using a preprocess function is the correct method in my opinion.
It might seem like a small thing, but separating your presentation and logic will stand you in good stead for future development. If you don't do it you'll end up with a messy code base and future maintenance will be harder than it needs to be :)