Passing anonymous functions and getting variables from an outer scope
Something like this would get you most of the way there:

&lt;?php
function h($val) {
    echo $val;
}

function template_function($template_body) {
    return function(array $template_output_variables = null) use ($template_body) {
        if ($template_output_variables !== null) {
            extract($template_output_variables);
        }
        $contents = eval('?&gt;'.$template_body.'&lt;?php');
        echo $contents;
    };
}


$contact_us = template_function('
    &lt;body&gt;
    Hello World Body Text, Hello &lt;?php h($name);?&gt;.
    &lt;/body&gt;'
);

$contact_us(array('name' =&gt; 'Johnny Boy'));


But it's evil, and you will likely end up with very odd, very hard-to-debug errors once your templates are of reasonable size/complexity. It's an interesting idea though -- if you pursue it, make sure you have a thorough set of test cases ;)

The easiest way to clean it up, IMO, would be to do away with allowing raw php in the templates. I'm pretty partial to Twig, you may be able to find some inspiration there.