Categories > TinyButStrong general >

template as string not as fileressource

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Johannes
Date: 2006-08-25
Time: 09:48

template as string not as fileressource

Hello TBS-Team at first: your template engine is my favorite engine - its so small and easy to use... and it works fine.

The only thing i added on my own is the possibility to use the TBS->LoadTemplate($template [,$isFileOrString])-Method with two parameters. Where the first gives the Template, which could be a File or a String and the second parameter is used to specify if its a file which is default or a string where you need a Flag.

it's easy to add and costs no performance.

So how do you think about it? - so i don't have to add it each version which is coming up!
best wishes from Germany
Johannes
By: TomH
Date: 2006-08-25
Time: 16:17

Re: template as string not as fileressource

You can do this already in TBS usin g the TBS->Source property.
From the manual
In order to load a template stored into a Php variable, you can code:
$TBS->Source = $my_template;
$TBS->MergeField('onload'); // force the merge of [onload] fields if any

In order to store the result at the end of the merging, you can code:
$TBS->Show(TBS_NOTHING) // terminate the merging without leaving the script nor to display the result
$result = $TBS->Source;

See also
http://www.tinybutstrong.com/forum.php?msg_id=2188

HTH,
By: Johannes
Date: 2006-09-16
Time: 00:42

Re: template as string not as fileressource

...this is a possibility, but i don't know how to solve the different behaviour (means htmlentities) from my solution.

in my solution i can use html-code without any problems, in your solution the html-code is encoded for presentation - means in my solution a " is a " in your solution a " is a & quot;
By: sheepy
Date: 2006-09-20
Time: 05:29

Re: template as string not as fileressource

You can tell tbs to do no conversion by LoadTemplate, then you can set the source to whatever you want.
By: Johannes
Date: 2006-09-21
Time: 21:27

Re: template as string not as fileressource

It definitely works not in the way i wan't it to work.

My Code is:
//load frames of the site
$my_template = file_get_contents('tpl/head.txt');
$my_template .= file_get_contents('tpl/'.$theme.'.txt');
$my_template .= file_get_contents('tpl/foot.txt');

//start templateEngine
$TBS = new clsTinyButStrong;
//use of my third parameter in TBS::LoadTemplate()
$TBS->LoadTemplate($my_template,FALSE,TRUE);
$TBS->Show();

my new Method of LoadTemplate() looks like:
function LoadTemplate($File,$HtmlCharSet='',$isstring=FALSE) {
    //some code...
    if (!tbs_Misc_GetFile($x,$File,$isstring)) return // [...]
    //some code...

tbs_Misc_GetFile() Method:
function tbs_Misc_GetFile(&$Txt,$File,$isstring=FALSE) {
  // Load the content of a file into the text variable.
  if($isstring === FALSE){
    //do business as usual
    $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;
  } else{
    //do my special business
    $Txt = $File;
    return true;
  } //if-$isstring-end
}

how do i rebild this functionality with yet implemented functionality?

Thank you for help
Johannes