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:
- The API documentation of the method (https://www.phpdocx.com/api-documentation/templates/replace-variable-word-fragment-Word-document):
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.
- The practical guide (https://www.phpdocx.com/documentation/practical/adding-content-to-a-template):
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.