OpenTBS - Repeating Paragraph with Token Replacement?

208 Views Asked by At

I have a text-based (as opposed to table-based) .docx document with a section that has several places in the paragraph for replacement. This paragraph must be duplicated and the tokens replaced for each entry in the array. I'm continuing to comb through the documentation but am kind of stuck on this one -- most of the examples are table-based and I'm not seeing how I can accomplish what I'm shooting for.

Here's an example of the section in the .docx file I am using as a template so far:

[onshow; block=begin; personsblock=tbs:p;]
Person 1
[flex_27.xx.01] [flex_27.xx.02], currently of [flex_27.xx.05], as amazing with the following: __{this has yet to be determined}__.
[onshow; block=end; personsblock=tbs:p]

... and my data so far is:

$personarray = array(); 
$personarray[] = array('tID27.01.01'=> 'Steve', 'tID27.01.02'=>'Klutcher' , 'tID27.01.05'=>'', 'tID27.01.06'=>'Cook'); 
$personarray[] = array('tID27.02.01'=> 'Tommy', 'tID27.02.02'=>'Boonary' , 'tID27.02.05'=>'Clarksville', 'tID27.02.06'=>'Montgomery'); 

... At this point, I am pretty much lost. I'll be programatically replacing the center 'code' (marked by xx) with the count of the person involved. What is the difference between a merge and a replace? How I combine the action? Can I do a multi-pass on the document somehow?

Sorry if these seem such basic questions but as I said, I've been stuck on this for two days.

1

There are 1 best solutions below

1
On BEST ANSWER

When you use block=begin ... block=end those is the syntax for absolute bounds. They indicate the position of the start and the end of the block in the source. In a doc, you don't have access to the inner XML source. So it is advised to use the relative syntax for blocs.

In PHP, your data structure should be like :

$personarray = array(); 
$personarray[] = array('c01'=> 'Steve', 'c02'=>'Klutcher' , 'c05'=>'', 'c06'=>'Cook'); 
$personarray[] = array('c01'=> 'Tommy', 'c02'=>'Boonary' , 'c05'=>'Clarksville', 'c06'=>'Montgomery'); 

In your DOCX, your template snippet should be like :

Person 1
[person.c01;block=tbs:p] [person.c02], currently of [person.c05], as amazing ...

Assuming that those two lines are in the same paragraph.

The expression [person.01;block=tbs:p] is a TBS fields that merges column '01' and also define the block bound as the paragraph that embeds this field.

They are also some problems in your original PHP spinnet :

  • Column names should not has dot (.) because this is a separator character for TBS field's names.
  • I use the prefix 'c' because PHP will turn numeric string key as numerical index.