Forum


Replies: 2   Views: 205
Adding lists using cloneblock
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 uncontested  · 23-11-2023 - 01:35

Hi, 

I'm trying to add a custom list style inside of a block element. This is my word document:

${LIST}

${BLOCK_SAMPLE}
${CONTENT}
${BLOCK_SAMPLE}

And then here is the code:

<?php

require_once '../../vendor/adm/phpdocx/src/Phpdocx/Create/CreateDocx.php';

$docx = new Phpdocx\Create\CreateDocxFromTemplate('document-2.docx');

$docx->setTemplateSymbol('${', '}');
$docx->createListStyle('latin', [
        ['type' => 'decimal',                'format' => '%1.'],
        ['type' => 'lowerLetter',    'format' => '%2.'],
        ['type' => 'lowerRoman',     'format' => '%3.'],
]);


$list = '<ul class="latin"><li><b>Item 1</b></li><li><b>Item 2</b></li></ul>';
$docx->replaceVariableByHTML('LIST', 'block', $list, ['customListStyles' => true]);


$word = new \Phpdocx\Elements\WordFragment($docx);
$word->createListStyle('latin', [
        ['type' => 'decimal',                'format' => '%1.'],
        ['type' => 'lowerLetter',    'format' => '%2.'],
        ['type' => 'lowerRoman',     'format' => '%3.'],
]);
$word->embedHTML($list, ['customListStyles' => true]);


$vars = [
        ['CONTENT' => 'Hello World 1'],
        ['CONTENT' => $word],
];


// clone block setting the variables to be replaced
$docx->cloneBlock('SAMPLE', 1, $vars, array('removeBlockPlaceholder' => true));

// delete remaining block
$docx->deleteTemplateBlock('SAMPLE');

$name = 'docs/output-'.rand();
$docx->createDocx($name);

header("Location: /word/".$name.".docx");

Unfortunately looks there something wrong. The first list looks good, but the one inside of the block is being rendered by ignoring the <li> tags. I tried adding the createListStyle in the word fragment but no luck. Here is the result:

----------------

  • Item 1
  • Item 2

Hello World 1

Item 1Item 2

----------------

Do you have any suggestion?

Thanks!

Posted by admin  · 23-11-2023 - 06:10

Hello,

cloneBlock does inline type replacements by default when using WordFragments (https://www.phpdocx.com/api-documentation/docx-path/clone-blocks-in-docx):

type string  Possible values are: inline (default) or block. Used by WordFragment values.

so block contents (such are paragraphs) are removed from the content, and only inline contents are added.
In this case you need to do a block type replacement to keep block and inline contents:

$docx->cloneBlock('SAMPLE', 1, $vars, array('removeBlockPlaceholder' => true, 'type' => 'block'));

We also recommend you create the new custom list style in the DOCX instead of the WordFragment, so it can be reused.

You can read more information about working with inline and block contents on https://www.phpdocx.com/documentation/practical/wordfragments-and-wordml (Tricks and tips section).

Regards.

Posted by uncontested  · 23-11-2023 - 16:22

Thanks! Worked pretty good.