Forum


Replies: 5   Views: 1310
Clone block and replace content
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 DC_Adams  · 20-07-2018 - 14:46

Hi there,

I am hoping that you could please assist me.

I am trying to clone a block and populate it with new data each time it is cloned.

Here is my code:
// code
$docx = new \Phpdocx\Create\CreateDocxFromTemplate('template.docx');

$names = [
  '<strong>Louis Coetzee</strong>',
  '<strong>Dillon Adams</strong>',
];

foreach ($names as $key => $item) {
  $docx->cloneBlock('NAME');
  $docx->replaceVariableByHtml('name', 'block', $item);
}

$docx->createDocx('clonedWord.docx');
// code

My template file:
// template file
$BLOCK_NAME$
$name$
$BLOCK_NAME$
// template file

The example shows that I set a block in the template file as specified in the documentation(I hope) and then I try to clone the block in a loop.

I want it to populate the name the way it is now with "Louis Coetzee" and then clone the block "NAME" and populate it with "Dillon Adams" 
so that in the word document it shows only the two names "Louis Coetzee" and "Dillon Adams"

At the moment it only displays the block name variables and Louis Coetzee inside that as described below:
// output
$BLOCK_NAME$
Louis Coetzee
$BLOCK_NAME$
// output

I think the issue is with the way that the block is being defined in the template file, I am just not 100% sure on how to set it. Any advice on getting it to work would be much appreciated.

I am using version 7.0 of phpdocx

Posted by admin  · 22-07-2018 - 11:32

Hello,

On https://www.phpdocx.com/documentation/cookbook/clone-blocks you can read how to work with blocks. Also please check the included samples in the package.

About your code, the problem is that you are cloning and replacing its content. We recommend you to do it using two loops:

foreach ($names as $key => $item) {
  $docx->cloneBlock('NAME');
}
foreach ($names as $key => $item) {
  $docx->replaceVariableByHtml('name', 'block', $item);
}

And use the firstMatch option available in the replace methods.

Anyway, we recommend you to upgrade to phpdocx 8.2 that includes many improvements to work with blocks. If you upgrade to this version, feel free to send to contact[at]phpdocx your template and the dev team will generate a custom code to illustrate how to do it.

Regards.

Posted by AurelienC  · 03-08-2018 - 13:16

Hello,

I am trying to achieve quite the same thing whitout success.

I have a basic template file :

$BLOCK_EXAMPLE$

Some not dynamic content.

Some dynamic content : $dynamic$

---

$BLOCK_EXAMPLE$

The cloning part works fine :

$datas = [];

for ($i=0;$i<5;$i++) {
    $docx->cloneBlock('EXAMPLE');
    $datas[] = 'number' . $i;
}

But the part where I try to populate all the differents $dynamic$ var with $datas doesn't :

foreach ($datas as $key => $item) {
    $docx->replaceVariableByHtml('dynamic', 'block', $item);
}

I end up with :

Some not dynamic content.

Some dynamic content : number0

---

(...)

---
Some not dynamic content.

Some dynamic content : number0

---

I don't know what I'm doing wrong.

And I couldn't find any example with blocks having dynamic content inside in them. I've only find examples with blocks and static content inside.

Thanks for your help.

Posted by admin  · 03-08-2018 - 16:02

Hello,

What version and license of phpdocx are you using?

Anyway, the code is working with the data you are adding. You are replacing all placeholders (with the same name):

$docx->replaceVariableByHtml('dynamic', 'block', $item);

with the same value. If you need to replace only the first one, you need to use the firstMatch option:

foreach ($datas as $key => $item) {
    $docx->replaceVariableByHtml('dynamic', 'block', $item, array('firstMatch' => true));
}

Using this option you can iterate an array, the first placeholder of the template is replaced, then the second one (as the first placeholder doesn't exist, this second placeholder is now the first occurrence) and so on.

Regards.

Posted by AurelienC  · 27-08-2018 - 09:48

Thansk for your reply, it works with the "firstmatch" method.

However, I still have trouble on how to order the blocks.
Let's say I have this template :

$BLOCK_uppercase$

I AM UPPERCASE

$BLOCK_uppercase$

$BLOCK_lowercase$

i am lowercase

$BLOCK_lowercase$

With this code :

$docx->cloneBlock('uppercase',1);
$docx->cloneBlock('lowercase',2);
$docx->cloneBlock('uppercase',3);
$docx->cloneBlock('uppercase',4);
$docx->cloneBlock('lowercase',5);
$docx->cloneBlock('lowercase',6);

I end up with : 

I AM UPPERCASE
I AM UPPERCASE
I AM UPPERCASE
i am lowercase
i am lowercase
i am lowercase

Instead of :

I AM UPPERCASE
i am lowercase
I AM UPPERCASE
I AM UPPERCASE
i am lowercase
i am lowercase

I might be using the "occurrence" parameter in the wrong way, I don't know.

PS : Version 7 and an advanced licence.

Posted by admin  · 27-08-2018 - 09:56

Hello,

The occurrence option of cloneBlock is available since phpdocx 8.2 . phpdocx 7 doesn't include it, and using this version the block placeholders must be unique; phpdocx 8.2 removes this limitation.

You can upgrade your license on MY PHPDOCX page after login. On https://www.phpdocx.com/news/ you can read the features included in each new version.

Regards.