Actually, by looking at how the big guys (meaning, real coders, unlike me
)cache their templates nowadays, you better create your plugin at the cache_templates hook and add this code or something equivalent:
PHP Code:
$globaltemplates = array_merge($globaltemplates, array('your_templatename'));
This is the only method that has worked for me and my custom templates for ad codes/banners.
Edit, your code looks more like the code to parsing it, which also could be better located at the parse_templates hook.