How to create a theme function for custom HTML markup and then call it in my node template?
You should be able to use the template_preprocess_node function to move your logic from your tpl files into the theme function. In order to use the function, replace template with the name of your theme.

function mytheme_preprocess_node(&amp;variables){
  if($variables['node']-&gt;type == 'my_content_type'){
    //change what you want to the node here.
  }
}


You could do the logic you need to inside this functionn, save the output to a variable in $variable (it's a reference var, so it dosn't need to be returned) and then just print out that variable in your tpl.php file.