Categories > TinyButStrong general >

php file as template

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Mario Miele
Date: 2005-11-23
Time: 13:40

php file as template

hi,

May I use a PHP file instead of an HTML file as template?
I always use almost the same structure for the web handling of mysql contents, so i'd like to write a PHP template (well, a text file with php code in it) to get a php program!

Possible with TBS?
Something particular I should know?


By: Skrol29
Date: 2005-11-23
Time: 15:42

Re: php file as template

How strange, but possible.

I see some requirements.
You have to run the result of the merge using the eval() function, or to save it into a file and run it using inculde(), but this is a lost of execution time.

If you want to run the result of the merge using eval(), then the template must not have "<?php" and "?>" tags.

In order to catch the result of the merge, then you have to avoid the default behavior of the Show() method, which does output the result and the merge and exit the script.

Example:
$TBS->LoadTemplate();
...
$TBS->Render = TBS_NOTHING; // Avoid the Show() command to output and quit
$TBS->Show(); // Merge automatic fields
eval($TBS->Source); // Run the result od the merge
By: Mario Miele
Date: 2005-11-23
Time: 16:41

Re: php file as template

dear skrol29,

I don't want to directly execute the result of the merge, but to save it as a php file (copy and paste the result should be ok). In other words, i want to use TBS as a kind of code generator, calling it on my personal web server.
By: Skrol29
Date: 2005-11-23
Time: 17:09

Re: php file as template

Ok, so the principle is the same.
Instead of eval(), use:
file_put_contents($filename,$TBS->Source);
This for Php 5. Otherwise you have to use fopen() and fwrite().
By: Mario Miele
Date: 2005-11-23
Time: 17:29

Thanks

Thanks
By: Wil
Date: 2005-12-13
Time: 11:46

Re: php file as template

>This for Php 5. Otherwise you have to use fopen() and fwrite().

OR use "PHP_Compat"

http://us3.php.net/manual/en/function.file-put-contents.php#42551

http://pear.php.net/package/PHP_Compat