i'm having a little trouble with this template deal. i have it setup so that i can put variables in the template so that it will be replaced later with what that variable represents. for example my navigation menu. problem is the only way i figured to do this is to read the contents of the file into a variable. but then all i can do with it is just print that out onto the page. so of course that php never gets executed. is there a way around this? i'm sure i'm just doing something wrong. i think i just need a new solution. any ideas?
parse php in template
Break it apart at the <?php tags and the ?>, and then use exec() on every odd (assuming it starts with HTML) index in the split array.
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.]