Forum


Replies: 3   Views: 1242
How can i scope template replacements within a block?
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by bkdl  · 08-07-2021 - 21:52

Hello,

How can I scope a template replacement, let's say replaceVariableByText, to only operate within a specific block? Or even scope it such that it won't operate within a block?

I'm familiar with the approach of cloning a block N times and then iterating over the records with firstMatch true to fill in each block, but in this case there are duplicate variables in the block. Our templates are user-configured and we can't make many assumptions about the variables.

It would be great if there were something like replaceVariableByText(block_name, variables, options).

Any thoughts on how I might accomplish this?

We have the premium license so any features at that level are available to us.

Example

User Pet List

name: $NAME$
username: $USERNAME$

Puppies
$BLOCK_PUPPIES$
[pic]
$NAME$

Stats
$NAME$
$AGE$
$WEIGHT$

Favorite foods
$BLOCK_PUPPY_FAVORITE_FOODS$
[pic]
$FOOD_NAME$
$PRICE$
$BLOCK_PUPPY_FAVORITE_FOODS$
$BLOCK_PUPPIES$

Posted by bkdl  · 08-07-2021 - 23:56

(The example has a nested block. I'm not even sure if that's possible.)

Posted by admin  · 09-07-2021 - 06:37

Hello,

The replaceVariableByText method (and other template methods) does replacements in the whole target  and as you say you can use the firstMatch option to replace only the first occurrence and iterate placeholders with the same name by their specific new values. Using the firstMatch option is the best approach to do replacements when cloning blocks: https://www.phpdocx.com/documentation/snippets/clone-blocks-replacing-placeholders.

There's no specific method to do replacements within a specific block, and as you also say you have nested blocks and the block names and their contents could be repeated more than once as well.

The easiest approach would be using specific placeholder names for each variable type instead of using the same placeholder name for more than one variable (for example, you are using $NAME$ for three different placeholders: user, pic and stats). Using specific placeholder names you could use the firstMatch option to iterate the placeholders in order after cloning the blocks.

As alternative approach you could also use replaceWordContent available in DOCXPath to do complex replacements. Using this method you can do almost any kind of replacements, for example: the second occurrence of a paragraph that contains $VAR$, or the placeholder that its paragraph also contains other text content, or more advanced replacements such as the third occurrence of a placeholder that is inside the second occurrence of a specific block. DOCXPath allows working with the internal options and also custom XPath queries. We recommend you to read the available documentation and the samples included in the package.

For further support, please send to contact[at]phpdocx.com a DOCX template sample and the DOCX output you'd like to get, the dev team will check them. Maybe a specific development could be done to ease your specific task.

Regards.

Posted by bkdl  · 09-07-2021 - 13:50

Thank you! I figured this case might require a lower-level action with DOCXPath. We can definitely go that route, but we'll try to make placeholders unique as recommended.