Search code examples
phpdocxopentbs

PHP/OpenTBS/DocX nested arrays / subblock


Actually testing opentbs to generate a docx based on a template from my website. I'm facing a problem integrating images. I succeeded to add images inside a loop, but my problem is integrating those in a nested loop.

I have an array of days Object and inside this an array of images path (with additional datas in each days)

My data schema is like this:

// Initialisation du Parser
$TBS = new clsTinyButStrong;
$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);

// Chargement du fichier Modèle
$TBS->LoadTemplate(realpath(dirname(__FILE__)).'/tbs/rendu-v2.docx', OPENTBS_ALREADY_UTF8);

$steps = array(
  stdClass {
    day: 'day 1',
    name: 'My 1st step',
    images: array(
      'pathToMyFirstImage',
      'pathToMySecondImage'
    )
  },
  stdClass {
    day: 'day 2 & 3',
    name: 'My 2st step',
    images: [
      'pathToMyThirdImage',
      'pathToMyFourthImage'
    ]
  }
);

$TBS->MergeBlock( 'steps2', $steps);
$TBS->Show(OPENTBS_DOWNLOAD);

In my word Template:

[steps2;block=begin]
  JOUR [steps2.day;sub1=images] : [steps2.name]
  [steps2.date]

  [steps2.description]

  [steps2_sub1; block=begin]
    [X] (picture with [steps2_sub1.val;ope=changepic;tagpos=inside] as text sustitution)
  [steps2_sub1; block=end]
[steps2; block=end]```

Actually, in the output file, my subblock is display as this and not interpreted.

I looked at this solution OpenTBS - How to use a double loop but end part is a little Strange and not working either with only [steps2_sub1;block=end].

OpenTBS doc doesn't give full example of subblocks with block.

Does anyone see what I misunderstand?

EDIT : My subblock doesn't work and is displayed as "code" and not interpreted.

Thanks,

Mathieu


Solution

  • It appears that TBS is space sensitive : I had a space before semicolon of sub1 in my begin block (MS Word add it automaticaly in FR language).

    Working code is next :

    [steps2;block=begin;sub1=images]
    [STEPS2.DAY] : [STEPS2.NAME]
    [steps2.date]
    
    [steps2.description;strconv=no]
    
    [X] <- Image with "[steps2_sub1.val;ope=changepic;tagpos=inside;block=tbs:p]" as Text Replacement
    
    [steps2;block=end]
    

    Thanks all

    By the way, how can I manage various way of displaying my pictures? I'll have 0 to 4 pictures to display like that :

    |    My Page     |
    |                |
    | Case 1 image   |
    [   100% Width   ]
    |                |
    | Case 2 images  |
    [  50%  ][  50%  ]
    |                |
    | Case 3 images  |
    [  50%  ][  50%  ]
    [      100%      ]
    |                |
    | Case 4 images  |
    [  50%  ][  50%  ]
    [  50%  ][  50%  ]
    

    Is it possible to achieve something like this?