Forum


Replies: 2   Views: 108
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 daveiroquois  · 18-05-2022 - 12:29

I'm trying to insert a dynamically generated ordered list into a word document, but simply doing addList() just generates the first 4 entries, and doesn't look like the rest of the document (I didn't apply any styles but I don't think that would fix the fact that only 4 are being shown).

I was then wondering if there was any way to insert an ordered list via replaceVariableByText. If so, what is the proper synatx for Word to recognize the data as an ordered list? Thanks!

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.

Posted by daveiroquois  · 19-05-2022 - 16:39

Looks like that did it. The 4 item thing was operator error, nothing due to phpdocx.