Categories > TinyButStrong general >

Execute php-code from templates

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Niklas K.
Date: 2006-07-27
Time: 21:00

Execute php-code from templates

Hello,

is there any chance to parse PHP Code which is in a template? I need it to code a CMS with
PHP-in-page-Support. I don't want to include external files with the TBS-function for it or an
iFrame...

Greetings,
Niklas K.
By: TomH
Date: 2006-07-27
Time: 21:26

Re: Execute php-code from templates

Noit only is that easy -but- you can also cache those TBS/php code scripts separately.

Take a careful look at the TBS example page ref "Execute another script" for full details. The short of it is
TBS PHP code
$test = 'tbs_us_examples_script1.php' ;
HTML template
[var.test;script=[val];subtpl]

There is example of the caching of subscripts -and- a prototype TBS CMS at http://tomhenry.us/tbs3/

HTH
By: Niklas K.
Date: 2006-07-27
Time: 21:36

Re: Execute php-code from templates

Yes, I know this method, but I said I don't want to use it. It is because I don't want to write files.
By: TomH
Date: 2006-07-28
Time: 00:46

Re: Execute php-code from templates

Then it seems you are saying you want to do not templates -- as TBS templates contain not native PHP only TBS processing directives.

If you don't want to write files - you still need the PHP code to be somewhere - why not put the PHP in the main file that calls the templates?
By: Niklas K.
Date: 2006-07-28
Time: 08:59

Re: Execute php-code from templates

I can't do that because I want to develop a CMS where you can install php code... must I switch to Smarty to make a system like this:

<html>
.....
[var.content]
.....
</html>

$content gets its content from a MySQL-Row where the id is the $_GET['id']. And I have a row like this:

pageid: 1 title: a simple php test name(that's for the menu): php... content: <?php echo 1+1; ?>

I hope you understand me now.

Can I use eval(); to do this?
By: Skrol29
Date: 2006-07-28
Time: 11:20

Re: Execute php-code from templates

Hello,

You could a use parameter "onformat" to apply a custom function on the content you want. This custom function could run the content as a php snippet using eval() and catch the result using ob_start() / ob_get_contents(). You need to catch the result in order to have it placed on the TBS tag instead of immediatly to client.

By: jahbini
Date: 2006-08-14
Time: 21:35

Re: Execute php-code from templates

In the bouwels of the TBS system resides the function that opens system files.  It is blessedly small and seems to be the only gateway for opening a system file.

A simple addition to it will allow any 'filename' that begins with ']' (Technically that's a legal beginning char for a file name, but you won't find too many files beginning with it, so use at your own pleasure/peril)

I'm putting this into my system for templates, since I'm working with a system where the templates would be pulled out of SQL and are not really in the files at all.

Anyway here it is, and I hope it would be generally useful to the TBS community:
function tbs_Misc_GetFile(&$Txt,$File) {
// if the filename begins with the closing char, we simply return the rest as the contents
        if(strncmp ($File,$this-> _ChrClose,Len($this ->_ChrClose) ) ==0 )                  
                { $Txt= substr($File,Len($this->_ChrClose)); return true; }
// Load the content of a file into the text variable.
        $Txt = '';
        $fd = @fopen($File, 'r'); // 'rb' if binary for some OS
        if ($fd===false) return false;
        $fs = @filesize($File); // return False for an URL
        if ($fs===false) {
                while (!feof($fd)) $Txt .= fread($fd,4096);
        } else {
                if ($fs>0) $Txt = fread($fd,$fs);
        }
        fclose($fd);
        return true;
}