Practical phpdocx

Adding content to a template

Introduction

To show how the library works with templates, let's modify a template and replace some variables for text, lists, tables and other contents.

Loading the template

First, create the new document instancing the class CreateDocxFromTemplate:

Now, let's replace the contents of this $docx object. The order of replacement is not relevant.

Texts

The easiest element for replacing a placeholder is a string. replaceVariableByText is the method to be used:

You have to set one or more value arrays here to replace them. A second parameter is available with the following options

  • 'target': Where the variables for replacement are: document, header, footer...
  • 'firstMatch': Replaces the first occurrence only if it's true
  • 'parseLineBreaks': Replaces all characters ‘\n’ with line breaks

For example, replace the TEXT placeholder by a text with a line break:

Lists

Replacing lists is as simple as adding a list with a single element in the template which contains the placeholder. When adding the new values, lists elements remain intact.

To add values to the list, use the replaceListVariable method:

Where the available parameters are, in order:

  • $variable: Name of the placeholder of the template
  • $listValues: Array with the values to add
  • $options: With the 'firstMatch' option replaces only the first occurrence if it's true, 'parseLineBreaks' replaces the characters '\n' with line breaks, 'target' set the placeholder target.

For example, switch the placeholder LIST in a web browser list:

Tables

Just like with the lists, you can add tables to the template by making a table with the styles that you want and define a row with the variables to replace. This row will be replaced for as many rows as needed with new values, keeping the original table styles.

The signature of the replaceTableVariable method is:

Where the parameters are, in order:

  • $vars: Multidimensional array with the table placeholders and its values. For each position of the array, a new row is created in the table.
  • $options: With the 'firstMatch' option replaces only the first occurrence if it's true, 'parseLineBreaks' replaces the characters '\n' with line breaks, 'target' set the placeholder target.
Deleting placeholders

Sometimes there's no need to replace every placeholder, and some of them remain in the template. To erase them, call removeTemplateVariable:

The available options are:

  • $variableName: Name of the placeholder to erase
  • $type: Delete type. It also removes or not the container paragraph
  • $target: To define the target of the placeholder as document, header, footer...

For example, to delete the placeholder FOOTERVAR in the footer code this method:

And, for deleting the placeholder OTHERVAR in the document:

Tips and Tricks

How to retrieve variables with the getTemplateVariables: at any time you can know which placeholder hasn't been replaced with the getTemplateVariables method. The method sends back an array with the variables by document, header, footer, footnote, endnote or comment targets.

Working with prefixes allows to carry an extensive control of every placeholder, divided by groups of work, and even permits to classify them by substitution priority. This makes easier its further replacement and deletion.

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.

Other methods are available to work with templates. They are detailed on the API documentation pages.

Next - Headers and Footers