I'm guessing your problem is that you need to register these variables for use in your custom template.
Just after the first line (before the render line) try this in your plugin:
PHP Code:
$templater->register('threadid', $threadid);
That's telling it to register the threadid variable to be used inside your custom template. It's a bit of a pain in the backside having to register already existing variables I know but that should be what you need. I know when I first looked at the system I just expected these variables to work straight away lol.