Forum


Replies: 3   Views: 909
Can you add font stylings on template variable replacements?
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  · 24-03-2022 - 18:24

Hello,

By default the replacements are done in the document (body) target. If you want to replace placeholders in other targets you need to set the target option available in template methods.
In the samples included in the examples/Templates/replaceVariableByWordFragment folder of all packages you can find sample codes that use this option. For example, from sample_5.php:

// replace the text variable in headers
$docx->replaceVariableByWordFragment(array('VAR_HEADER_1' => $imageHeader, 'VAR_HEADER_2' => $textHeader, 'VAR_HEADER_3' => $textOther), array('type' => 'inline', 'target' => 'header'));

// replace the text variable in the document
$docx->replaceVariableByWordFragment(array('VAR_BODY_1' => $textBody1, 'VAR_BODY_2' => $textBody2, 'VAR_BODY_3' => $imageBody));

// replace the text variable in footers
$docx->replaceVariableByWordFragment(array('VAR_FOOTER_1' => $textFooter, 'VAR_FOOTER_2' => $imageFooter), array('type' => 'inline', 'target' => 'footer'));

You can read about this option on:

target    string  Possible values are: document (default), header, footer, footnote, endnote, comment. This option sets the scope of the replacement procedure.
By default, methods that replace placeholders in templates like replaceVariableByText or replacePlaceholderImage substitute the existing placeholders in the body of the document with new contents.

If you need to replace placeholders in other targets, use the target option, available in all methods. This way, it is possible to exchange placeholders in any target: document, header, footer, footnote, endnote and comment.
Template methods replace placeholders in the main document body. To replace placeholders in other scopes such as header, footer, comment or footnote, the target option is available.

If you want to replace the placeholders in all targets you can iterate all targets. The previous cookbook page details the following code using replaceVariableByText, but you can use other template methods:

foreach (array('document', 'header', 'footer', 'footnote', 'endnote', 'comment') as $target) {
  $docx->replaceVariableByText($variables, array('target' => $target));
}

Regards.