OpenTBS - How to use a double loop

爷,独闯天下 提交于 2020-01-06 23:50:32

问题


I am trying to create a block inside a sub block in a .docx using openTBS, with no success so far.

Here is my PHP array:

$myBlock = array(
   0 => array(
          'description' => 'description1',
          'name'        => 'name1',
          'photos'      =>  array (
                0 => array(
                   'path' => 'C:\path_to\pic_0000y.png',
                   'name' => 'photo0000'),

                1 => array(
                   'path' => 'C:\path_to\pic_1111y.png',
                   'name' => 'photo111'),
                ), // end of photo array
       ),  // end of first element 

   1 => array(
          'description' => 'description2',
          'name'        => 'name2',
          'photos'      =>  array (
                0 => array(
                   'path' => 'C:\path_to\pic_3333y.png',
                   'name' => 'photo3333'),

                1 => array(
                   'path' => 'C:\path_to\pic_00000.png',
                   'name' => 'photo00000'),
                ), // end of photo array
       ),  // end of second element
);

Which I am merging into TBS like this:

$this->TBS->MergeBlock( 'myBlock', $myBlock);

An equivalent in php of what I am trying to achieve:

foreach( $myBlock as $myBlockKey => $myBlockData)
{
  echo $myBlockData['description'];
  echo $myBlockData['name'];

  foreach( $myBlockData['photos'] as $photoKey => $photoData)
  {
     echo $photoData['name'];
     echo $photoData['path'];
  }
}

Which would display:

description1
name1
photo0000
C:\path_to\pic_0000y.png
photo111
C:\path_to\pic_1111y.png

description2
name2
photo3333
C:\path_to\pic_3333y.png
photo0000
C:\path_to\pic_0000y.png

I guess there is not much point of posting wrong things that I have tried, but that is pretty much what I tried to do:

[myBlock; block=begin;]
      [myBlock.description]
      [myBlock.name]

      [myBlock.photos; block = begin;]  // where it fails
          [myBlock.photos.path]         // where it fails
          [myBlock.photos.name]         // where it fails
      [myBlock.photos; block=end;]      // where it fails

[myBlock; block=end;]

回答1:


You have to use the TBS automatic sub-block feature.

[myBlock; block=begin;sub1=photos]
      [myBlock.description]
      [myBlock.name]

      [myBlock_sub1; block = begin;]
          [myBlock_sub1.path]
          [myBlock_sub1.name]
      [myBlock_sub1.photos; block=end;]

[myBlock; block=end;]

Your syntax with [myBlock.photos;block = begin] cannot work because (1) the block bounds for [myBlock] has already been defined, so parameter "block" in this tag is ignored. And (2) [myBlock.photos] is an item which is an array so it cannot be displayed.



来源:https://stackoverflow.com/questions/35164365/opentbs-how-to-use-a-double-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!