tags:

views:

72

answers:

3

Hi guys, i have 1 template-file, which is included into main file.

some.template:

...
<?php echo '!'; ?>
...

in main file i read file into 1 variable and display it. Of course, php script doesn't run there. How can i do that, what i want?

Thanks

A: 

Do not read the file into variable, instead do

 include 'some.template';
Anti Veeranna
I have to edit template before inserting. include is wrong way.
Ockonal
Rewriting the PHP code in template? You really should rethink your design. If you could give more details about what exactly it is that you want to achieve, somebody can perhaps offer a better solution.
Anti Veeranna
+3  A: 

You could use include to read and process the file. If you need to post-process the result (I assume that might be the reason for you to read the template into a variable) try output buffering:

ob_start();
include 'some.template';
$out = ob_get_clean();
echo $out;

If you need to modify the template before having PHP process it, you will have to resort to eval. But I would advise you to be very careful with this for obvious security reasons and maybe-not-so-obvious performance reasons. If you just need some variables to be replaced, you should consider using a template engine instead or simple means like preg_replace.

cg
A: 

Since you wish to edit the script before using it, the only way to do this practially is to use:

eval($string);

I'm sure I don't need to tell you not to do this with user input if you value security at all.

Antony Carthy
I'm sure that you do need to tell him that it is insecure, too many people are poor programmers to assume otherwise with a question like this.
The Wicked Flea