Forum


Replies: 3   Views: 348
Replacevariablebyhtml css not applying
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 jennygj  · 26-02-2024 - 14:09

Hi

Could you advise why the styles in $testValue may not be applied during docuemnt creation?

$placeholder = '$EXAMPLE$';

$testValue = '<style>p {color:red; font-weight:bold;}></style><p>Test HTML</p>';

$docx->replaceVariableByHTML($placeholder, 'inline', $testValue, ['addDefaultStyles' => false]);

Posted by admin  · 26-02-2024 - 15:19

Hello,

Please note that the addDefaultStyles option prevents adding some styles. Also please note that the placeholder names to be replaced don't include the template symbols.

Please use the following code:

$placeholder = 'EXAMPLE';

$testValue = '<style>p {color:red; font-weight:bold;}></style><p>Test HTML</p>';

$docx->replaceVariableByHTML($placeholder, 'inline', $testValue);

All styles must be applied correctly now.
If you send to contact[at]phpdocx.com the template you are using, we can generate a custom code using it.

Regards.

Posted by jennygj  · 26-02-2024 - 20:01

"Please note that the addDefaultStyles option prevents adding some styles."

When you say 'some styles', which specific styles does it prevent being added? 

Posted by admin  · 26-02-2024 - 20:49

Hello,

If you enable addDefaultStyles the following styles aren't added: paragraph spacings, paragraph indentation, list text alignment, font-family, color, vertical-align and font-size. This option was included years ago to prevent adding some styles, so its use is very specific. Instead of using this option, we recommend you to apply the needed CSS styles and also use HTML Extended and CSS Extended if needed (https://www.phpdocx.com/documentation/introduction/html-extended-to-word-PHP).

For example, CSS Extended includes data-font-size: "initial" to avoid applying a font size and use the default value (https://www.phpdocx.com/htmlapi-documentation/html-standard/insert-paragraph-text-Word-document-with-HTML).

Regards.