Replies: 3   Views: 117
Docx variable using replacevariablebywordfragment
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 Exorion  · 07-05-2022 - 11:33

Hi For example if i have bold $VARIABLE$ and make just replace Variable By Text - everything in format is saved (bold, itallic etc) if i use replaceVariableByWordFragment - i can't be sure if format is still the same. I need to detect somehow is it bold or not (for example) and add it to a format of WordFragment.

Is it a easier way to save format of variable written and applied it to a WordFragment somehow?


Posted by admin  · 07-05-2022 - 19:11


When a placeholder is replaced by a WordFragment, the tag container of the placeholder is replaced, including the inline styles of the tag. If the styles are applied to the parent element of the element (for example paragraph styles when doing an inline type replacement) then these styles of the parent remain. This doesn't happen when only the text is replaced as using replaceVariableByText (and other replace methods),

Using a Basic or an Advanced license you need to set the styles to be applied in the new content created with the WordFragment.

Premium licenses include the stylesReplacementType option in the replaceVariableByWordFragment method to apply the placeholder styles or mix the styles from the new content and the styles from the placeholder when doing the replacement.


Posted by Exorion  · 12-05-2022 - 03:39

Is there any way to detect current style of variable? just to put it on the ReplaceWordFragment?

Posted by admin  · 12-05-2022 - 05:52


The getWordStyles metod return styles used by contents, but its structure can't used directly with WordFragments, you'd need to parse the styles and generate and array with the styles to be applied.

The recommended approach to autodetect and apply the existing styles in a placeholder when replacing it by a WordFragment is using a Premium license with the stylesReplacementType option.