TinyButStrong - the PHP Template Engine
Categories > OpenTBS general >

Impossible to use multiple templates

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs

By: Vaarteen
Date: 2014-06-18
Time: 12:35

Impossible to use multiple templates

Hi Skroll.

I'm using TBS with OpenTBS with pleasure for years now but today I founded something I will qualify as a bug.
I'm merging 4 requests in 3 different Excel worksheets.
So I create 3 instances of TBS and in a loop I merge each block in each template.
And it doesn't work.

The fault goes to the method MsExcel_ReplaceString(&$Txt, $p, &$PosEnd) which uses a static array notbs[].
So notbs[] is a class-wide variable and exists in each instance.

A workaround is to declare notbs as an instance-wide variable in the constructor, that's what I did.

The diff :
# This patch file was generated by NetBeans IDE
# It uses platform neutral UTF-8 encoding and \n newlines.
--- <html>tbs_plugin_opentbs_1.8.1.beta.php (<b>Today 11:05:30</b>)</html>
+++ <html><b>Current File</b></html>
@@ -62,6 +62,11 @@
  */
class clsOpenTBS extends clsTbsZip {

+  function __construct() {
+    parent::__construct();
+    $this->notbs = array();
+  }
+
     function OnInstall() {
         $TBS =& $this->TBS;

@@ -2789,7 +2794,6 @@
         static $v1_len = 3;
         static $v2 = '</v>';
         static $v2_len = 4;
-        static $notbs = array();

         // found position of the <c> element, and extract its contents
         $p_close = strpos($Txt, $c, $PosEnd);
@@ -2807,12 +2811,12 @@
         // extract the SharedString id, and retrieve the corresponding text
         $v = intval($vt);
         if (($v==0) && ($vt!='0')) return false;
-        if (isset($notbs[$v])) return true;
+        if (isset($this->notbs[$v])) return true;
         $s = $this->OpenXML_SharedStrings_GetVal($v);

         // if the SharedSring has no TBS field, then we save the id in a list of known id, and we leave the function
         if (strpos($s, $this->TBS->_ChrOpen)===false) {
-            $notbs[$v] = true;
\ No newline at end of file
+            $this->notbs[$v] = true;
\ No newline at end of file
             return true;
         }
By: Skrol29
Date: 2014-06-19
Time: 00:17

Re: Impossible to use multiple templates

Hi Vaarteen,

Thank for sharing this issue.
I'm currently working a the next version and I will fix this problem.

Regards,


By: Vaarteen
Date: 2014-06-19
Time: 17:15

Re: Impossible to use multiple templates

You're welcome.

I'm afraid there's a lot of these static things lurking around in the code.
I didn't dig  a lot, but I think most of them if not all should either be instance-wide or not static at all.

If that can help, here is what I guess (again, I didn't really search through all the code, but as these vars are only used locally I think I'm right) :

- these static vars should be instance-wide :
function OnCommand() -> $img_num
function TbsDebug_Init() -> $DebugInit
function TbsPicAdd() -> $index and $internal
function MsExcel_ReplaceString() -> $notbs
function MsExcel_ChangeCellType() -> $OpeLst
function OpenDoc_ChangeCellType() -> $OpeLst

- These static vars shouldn't be static at all :
function MsExcel_ReplaceString() -> $c, $v1, $v1_len, $v2 and $v2_len
function MsExcel_ChangeCellType() -> $v
function OpenDoc_ChangeCellType() -> $TypeLst
By: Skrol29
Date: 2014-06-19
Time: 19:47

Re: Impossible to use multiple templates

Hi,

I agree, the code needs a review on static variables.

But it is not as extended as it could seems.
The problem of  using several instances of TBS and even re-use the same instance (this theoretically possible) has been taken in account since several versions.
The problems that remains is from the old code.
Since several versions, OpenTBS has a method TbsInitArchive() that manages the initialization of the template, for a new instance or the same instance.

For example, ->notbs should be in initialized in TbsInitArchive().