Forum


Replies: 2   Views: 145
Use lists in templates
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 admin  · 18-05-2022 - 14:40

Hello,

The replaceVariableByText method replaces a text placeholder by a new text, not other contents.

You can use replaceListVariable to add contents in lists with a placeholder in a DOCX template. For example (template included in the package):

$docx = new CreateDocxFromTemplate('TemplateList.docx');

$items = array('Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6');

$docx->replaceListVariable('LISTVAR', $items);

If you want to replace a text placeholder with a list you need to use replaceVariableByWordFragment. For example (template included in the package):

$docx = new CreateDocxFromTemplate('TemplateWordFragment_1.docx');

// create the Word fragment that is going to replace the variable
$wf = new WordFragment($docx, 'document');

$itemList = array(
    'Line 1',
    'Line 2',
    'Line 3',
    'Line 4',
    'Line 5',
    'Line 6',
    'Line 7',
    'Line 8',
    'Line 9',
    'Line 10',
);

// set the style type to 1: unordered list
$wf->addList($itemList, 1);

$docx->replaceVariableByWordFragment(array('WORDFRAGMENT' => $wf), array('type' => 'block'));

$docx->createDocx('example_replaceVariableByWordFragment_1');

Using WordFragments require applying the styles to be used if you don't want to use the default ones from the document.

The addList method works perfectly too:

$docx = new CreateDocx();

$itemList = array(
    'Line 1',
    'Line 2',
    'Line 3',
    'Line 4',
    'Line 5',
    'Line 6',
    'Line 7',
    'Line 8',
);

// set the style type to 1: unordered list
$docx->addList($itemList, 1);

$docx->createDocx('example_addList_1');

We have done some quick tests using addList as standalone method and as WordFragment with more than 100 items and all are added correctly. There's no limit in the number of items when generating a list.
We don't know why you are getting only the first 4 entries, maybe some content is overlapping other items in the DOCX template you are using or there's something wrong in the code you are running; if you post the code you are using we can check it (or if you send to contact[at]phpdocx.com the code and the DOCX template you are running).

All packages include samples of all methods (examples folder). We recommend you run them and check the outputs of the following samples: Core/addList and Templates/replaceListVariable folders.

Regards.