Correction; eval() - similar names, similar jobs, but exec() runs executables and eval() runs PHP scripts.
Actually, it should be possible to run the whole page in a single eval()
eval('?>'.$template);
The "?>" makes PHP drop out of PHP mode, so that it won't get upset when it sees the "<?php" in the template string. After that, assuming the <?php and ?> are properly paired off in $template, it should run as expected.
As an example of the sort of thing I'm guessing you mean, a sample template might look something like:
First, a chunk of non-PHP.
<?php
echo "This is from PHP.\n";
?>
This is not.
<?php
echo "But this is.\n";
?>
And this is <?php echo "mos"."tly"?> non-PHP.
(not bothering to actually write HTML, you'll notice, only valid, if trivial, PHP).
Get all that into a string, pass through the eval() call above, and the output I get is:
First, a chunk of non-PHP.
This is from PHP.
This is not.
But this is.
And this is mostly non-PHP.
[Note: if the $template has bugs, the error message will refer to the line in the script where the eval() appears.]