Forum


Replies: 1   Views: 466
How to apply list level override when importing html
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 mcaldas  · 21-07-2021 - 03:42

I'm using the replaceVariableByHTML function in order to insert the following html into a Word template:

<ul>
<li>Countries
<ol style="list-style-type: lower-alpha;">
<li>Argentina</li>
<li>Brasil</li>
<li>Colombia</li>
</ol>
</li>
<li>Cities
<ol style="list-style-type: lower-alpha;">
<li>Alabama</li>
<li>Baltimore</li>
</ol>
</li>
</ul>

The html shows two inner lists (countries and cities) starting at letter 'a' (lower-alpha). But when I see the resulting Word it shows the first OL list (countries) at 'a' and the second OL list (cities) at 'd'.

I was wondering if I should use a specific parameter

I'm using phpDocx 11 Premium and PHP 7.2

Posted by admin  · 21-07-2021 - 07:07

Hello,

As you want to generate distinct sublists (restarting the sublists) in a list, you need to create new lists or apply level override. Otherwise you'll get a continuous numbering. You need to use HTML Extended, available in Premium licenses.

You can accomplish the requested task closing the li tag before each sublist and setting the depth to be applied:

$docx = new CreateDocx();

$html = '
<ul>
    <li>Countries</li>
        <ol>
            <li data-depth="1">Argentina</li>
            <li data-depth="1">Brasil</li>
            <li data-depth="1">Colombia</li>
        </ol>
    </li>
    <li>Cities</li>
        <ol>
            <li data-depth="1">Alabama</li>
            <li data-depth="1">Baltimore</li>
        </ol>
    </li>
</ul>
';
$docx->embedHTML($html, array('useHTMLExtended' => true));

Or the recommended approach, that is using a custom list style with level override options. On https://www.phpdocx.com/documentation/introduction/html-extended-to-word-PHP (List level override section) you can read more information about this task and a sample.

The following sample illustrates how to do it:

// custom options
$listOptions = array();
$listOptions[0]['type'] = 'bullet';

// override
$overrideListStyleOptions = array();
$overrideListStyleOptions[0]['type'] = 'lowerLetter';

$overrideListStyle = array();
$overrideListStyle[0] = array(
    'listOptions' => $overrideListStyleOptions,
    'name' => 'letter',
);

// create the list style with name: clist
$docx->createListStyle('clist', $listOptions, $overrideListStyle);

$html = '
<ul class="clist">
    <li>Countries
        <ol class="letter">
            <li>Argentina</li>
            <li>Brasil</li>
            <li>Colombia</li>
        </ol>
    </li>
    <li>Cities
        <ol class="letter">
            <li>Alabama</li>
            <li>Baltimore</li>
        </ol>
    </li>
</ul>';
$docx->embedHTML($html, array('customListStyles' => true, 'useHTMLExtended' => true));

Regards.