API Documentation
Word content
Format conversion
CryptoPHPDOCX
Digital Signature package
replaceTemplateVariableByHTML
Replaces a template variable by HTML content.
PRO, PRO+ and CORPORATE PHPDocX >= 2.5
Description
public replaceTemplateVariableByHTML (string $var, string $type, string $html [, array $options])
This method allows for the insertion of HTML into a template. You may insert it as a ‘block’, e.g. removing the corresponding paragraph and substituting it by the block of WordML, or ‘inline’, e.g. just replacing the variable by inline WordML.
Practically all HTML tags and CSS styles are supported.
This method transforms HTML directly into WordML and it is compatible with OpenOffice and PDF conversion.
You may find a more detailed explanation of this useful method in the HTML to Word section of the API documentation.
Parameters
var
The template variable to be replaced by the HTML content.
type
The substitution type, it can be:
- ‘block’: the variable, together with its containing paragraph, is replaced by the HTML content or
- ‘inline’: only the variable is replaced by the inline HTML content.
html
The HTML code to be translated into WordML: it could be a string or the path to a file.
options
The posible keys and values are:
| key | Type | Description |
|---|---|---|
| isFile | bool | True for files and false for strings. |
| baseURL | string | The base URL used to complete the relative paths of links and images. |
| downloadImages | bool | If true inserts the images into the docx document, otherwise just links them as an external source. |
| filter | mixed | Could be an string denoting the id, class or tag to be filtered or an array with a list of filters, if you want only a class introduce .classname, #idName for an id or for a particular tag, if you use the plain name it will search in ids, classes and tags. |
| parseAnchors | bool | If true parses the anchors included in the HTML code. |
| parseDivsAsPs | bool | If true parses the div elements as paragraphs. |
| parseFloats | bool | If true preserves the floating properties of images and tables. |
| strictWordStyles | bool | If true ignores all CSS styles and uses the styles set via the wordStyles option (see next). |
| wordStyles | array | One may associate different Word styles to HTML classes, ids or tags. For example: array(’.myClass’ => ‘LightGridPHPDOCX’, ‘#myId’ => ‘LightListAccent6PHPDOCX’, ‘‘ => ‘PHPDOCXHeading1′). |
Return values
Void.
Code samples
Example #1: replaces template variables by inline and block HTML contents
require_once '../../classes/CreateDocx.inc';
$docx = new CreateDocx();
$docx->addTemplate('../files/testHTML2mdc.docx');
$docx->replaceTemplateVariableByHTML('ADDRESS', 'inline', 'C/ Matías Turrión 24, Madrid 28043 Spain
', array('isFile' => false, 'parseDivsAsPs' => true, 'downloadImages' => false));
$docx->replaceTemplateVariableByHTML('CHUNK_1', 'block', 'http://www.2mdc.com/PHPDOCX/example.html', array('isFile' => true, 'parseDivsAsPs' => true, 'filter' => '#capa_bg_bottom', 'downloadImages' => true));
$docx->replaceTemplateVariableByHTML('CHUNK_2', 'block', 'http://www.2mdc.com/PHPDOCX/example.html', array('isFile' => true, 'parseDivsAsPs' => false, 'filter' => '#lateral', 'downloadImages' => true));
$docx->createDocx('../docx/replaceTemplateVariableByHTML');
The resulting Word document looks like (download .docx file):
Change log
- Available since 2.5
Available examples
See also
PHPDocx. Dinamic generation of reports in .docx format 


