Categories > TinyButStrong general >

Subtemplate and obtained html code

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Jack.R
Date: 2005-09-15
Time: 22:43

Subtemplate and obtained html code

I have been very impressed by TinyButStrong during RMLL, so I am making some test on  how to use properly subtemplate.
I probably misunderstood something as, if I look at the source code of the obtained html page, I can see more than one time <html> <head> </head> <body>
I was expecting that TinyButStrong was including only subtemplate contents found between <body>and </body>
Could someone tell me where I make a mistake ?

Here is the code.

mymain.php5
<?php
include_once('./tbs_class_php5.php5');

// Setup
$header_tpl = 'myheader.html';
$footer_tpl = 'myfooter.php5';

// TBS process
$TBSmymain = isset($this) ? $TBSmymain = $this : $TBSmymain = new clsTinyButStrong;

$TBSmymain->LoadTemplate('mymain.html');

$TBSmymain->Show();
?>


mymain.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" />
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <title>TinyButStrong subtemplate test</title>
  </head>
  <body>
    <h1><center>TinyButStrong subtemplate test</center></h1>
    <p>
      All below that line came from subtemplates!
    </p>
    <table border="1" width="100%">
        <tr>
          <td>[var.header_tpl;file=[val]]</td>
        </tr>
        <tr>
          <td>[var.footer_tpl;script=[val];subtpl]</td>
        </tr>
    </table>
  </body>
</html>


myheader.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" />
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <title>Header</title>
  </head>
  <body>
    <h2>Static html page</h2>
    <p>
      Some text ...
    </p>
  </body>
</html>


myfooter.php5
<?php
include_once('./tbs_class_php5.php5');

$GLOBALS['phpversion'] = phpversion();

// TBS process
$TBSmyfooter = isset($this) ? $TBSmyfooter = $this : $TBSmyfooter = new clsTinyButStrong;

$TBSmyfooter->LoadTemplate('myfooter.html');

$TBSmyfooter->Show();
?>


myfooter.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" />
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <title>Footer</title>
  </head>
  <body>
    <p align="center">dynamically, we get : &nbsp;
      PHP [var.phpversion]&nbsp;&nbsp;&nbsp;
      TinyButStrong [var..version]&nbsp;&nbsp;&nbsp;
      &copy; myself 2005
    </p>
  </body>
</html>

By: Skrol29
Date: 2005-09-16
Time: 01:25

Re: Subtemplate and obtained html code

Hello Jack,


Parameter "file=" does keep only contents betwen body tags (if any is found). That's the default behavior. You can cancel it using patameter "htmlconv=no" which forces TBS to keep the contents as is.

That's different for Parameter "script". Because this parameter can produce html source in so many ways. It always insert the contents of the sub-template as is.

I can make a remark about your subscript myfooter.php:
When you use paremeter "script" with parameter "subtpl", there is no need to create a new TinyButStrong instance. There is a local variable called $TBS already available for you. In fact, it is a reference on your main TBS instance, but turned into a special mode. This saves lot of memory space, and process execution.

By: Jack.R
Date: 2005-09-16
Time: 07:05

Re: Subtemplate and obtained html code

Hello Skrol,

Ok, I don't have catch differency between file and script, for resulting code. Now it is clear.

Regarding subscript myfooter.php, I am new to PHP object programming, so may be I am wrong.
In my idea,
$TBSmyfooter = isset($this) ? $TBSmyfooter = $this : $TBSmyfooter = new clsTinyButStrong;
is creating a new TinyButStrong instance only if myfooter.php is called directly. When called as a subtemplate, $this exist and is simply assinged to $TBSmyfooter. Of course, I did not check how php react to $TBSmyfooter = $this (create or not a new instance)
The main idea for that way of doing is that each part of the final page can be developped and tested separately as a module, even by different developper, and then merge all together by the main template.

In the manual, you says that local variable in the subtemplate is $this with parameter script and $TBS with parameter onformat. Is it still the case or is it $TBS in both  cases ?

Thanks
By: Skrol29
Date: 2005-09-16
Time: 10:18

Re: Subtemplate and obtained html code

Hello,

>In the manual, you says that local variable in the subtemplate is
> $this with parameter script and $TBS with parameter onformat.
> Is it still the case or is it $TBS in both  cases ?

The manual is rigth. Use $this in subscript, not $TBS.

> is creating a new TinyButStrong instance only if myfooter.php
> is called directly.

Your rigth, I didn't see that tric.
Your subscript is correct.
By: Jack.R
Date: 2005-09-16
Time: 21:45

Re: Subtemplate and obtained html code

Hello,

Modified myfooter.php5 in order to get proper html code, even when called as subtemplate.
Could give some idea to somebody else ...
<?php
include_once('./tbs_class_php5.php5');

$GLOBALS['phpversion'] = phpversion();

// Check if called by another TBS instance
if (isset($this)) {
  $isModule = true;
  $TBSmyfooter = $this;
}
else {
  $isModule = false;
  $TBSmyfooter = new clsTinyButStrong;
}

// TBS process
$TBSmyfooter->LoadTemplate('myfooter.html');
$TBSmyfooter->Show(TBS_NOTHING);

// Get only body contents if called by another TBS instance
if ($isModule) {
  $TBSmyfooter->Source = tbs_Html_GetPart($TBSmyfooter->Source,'BODY',false,true);
}
echo $TBSmyfooter->Source;
?>
By: Jack.R
Date: 2005-11-02
Time: 22:24

Re: Subtemplate and obtained html code

Hello Skrol29,

After update from PHP 5.0.4 to PHP 5.0.5 on my Debian box, code previously proposed no more works. If I check resulting page html code, only footer.php5 result is output twice.

I make the test with unofficial release and official ones but get exactly same result. If I get back to PHP 5.0.4, it works again.

Any Idea ?

Best regards
By: Skrol29
Date: 2005-11-03
Time: 01:14

Re: Subtemplate and obtained html code

Hello Jack,

I can reproduce this behavior too.

It seems that Php 5.0.5 does't assign by reference when the value comes directly from a function.

The line that's make this result in your example is
  $this->Source =& tbs_Misc_UnlinkVar('');
If you change it with:
  $x = '';
  $this->Source =& $x;
then your example run ok.

This problem can produce other TBS strange behaviors because this technic is used several times in TBS. But in my point of view it is a Php 5.0.5 bug.
You can reproduce this bug with the following snippet:
<?php
$a = 'hello';
$b =& $a;
$a =& f_test();

echo "* a = {".$a."}, b = {".$b."}<br>";

function f_test() {
    return 'tested';
}
?>

With Php < 5.0.5 you have:
  * a = {tested}, b = {hello}
With Php = 5.0.5 you have:
  * a = {tested}, b = {tested}
By: Skrol29
Date: 2005-11-03
Time: 16:40

Re: Subtemplate and obtained html code

Hello Jack,

After a discussion with the PHP support (http://bugs.php.net/bug.php?id=35075) it happens that this is not a PHP bug.

So I've made a TBS patch which fixes this problem.
TBS 2.05.3 is now available for download.

Thanks for having posted this problem.
By: Jack.R
Date: 2005-11-03
Time: 20:53

Re: Subtemplate and obtained html code

Hello Skrol29,

Thanks a lot for so quick answer.
I got the 2.05.3 and now works great.

Thanks again