Practical phpdocx

Working with templates


Making a document from scratch is a complex task when, for example, you search for a very elaborate visual detail of the file or to customize its content.

To simplify the document creation process, phpdocx allows to use templates. In phpdocx a template is a DOCX file, which contains one or multiple variables to replace for content; e.g. a text, an image, HTML, or a graphic chart.

To work with templates you have to run the class CreateDocxFromTemplate, which provides all necessary methods to modify documents.

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.

As an example, let's create a document with a single variable and replace it with a text.

Setting up the template

phpdocx uses the symbol '$'' to set the variables to be replaced. Therefore, to define a variable with the name SIGN, add it to the document by writing $SIGN$. This way, you can define as much variables as needed, either in the content of the text, headers, footers, or in individual paragraphs, inside table forms, lists, or any other content inside the document.

If you prefer to work with another symbol, call the method setTemplateSymbol. For example, to use ‘#’ as symbol for templates, apply the code:

The only thing to consider is that the symbol has to be the same at the beginning and end. For example, it is not possible to use opening and closing curly brackets like in “{SIGN}”. The opening and closing symbol must be the same.

How to work with templates

The complete code to create the document using a template is the following:

After including the library, create a new instance of CreateDocxFromTemplate.

This class inheritances CreateDocx to provide all necessary methods to work with templates.

For this example, let's replace the variable SIGN for the text ‘phpdocx’. Type this code to do it:

After you set the text string, you have to create an array with the name of the placeholder you want to replace. Assign the new text to it, then, replace it with the method replaceVariableByText, and assign to it the array of the variables previously created.

Finally, create the new document with the file name ‘tutorial_3’ using the method createDocx. The original template is not modified at any time, remaining intact for future needs.

To use the namespaces version, include the main CreateDocx.php class and load the template:

or apply the following code to load the Autoloader manually:

The rest of the code lines remains similar to the ones previously explained.

Available methods for content replacement

The class CreateDocxFromTemplate provides multiple methods. They will be reviewed in detail later.

These methods allow to work with templates to modify, replace and delete the added variables.

Avoid adding strings with the same name than placeholders in the same paragraph.

Some of the most common methods are the following:

  • To retrieve all available variables, run the method getTemplateVariables.
  • The 'replace' method set for substituting variables for lists, texts, images, HTML, table forms, external documents, other files with DOCX, HTML or RTF format, or even Word fragments.
  • To work with sets of variables, texts, or any other content, choose cloneBlock, clearBlocks and deleteTemplateBlock.
  • To delete variables, call the method removeTemplateVariable.

DOCXPath eases low level work with documents, in order to insert, move, replace, clone and delete contents.

Next - WordFragments and WordML