Skip to content

Commit bcfb3e8

Browse files
committed
Add methods setValuesFromArray and cloneRowFromArray to the TemplateProcessor-class and update samples and docs accordingly
1 parent 38cb04d commit bcfb3e8

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

docs/templates-processing.rst

100644100755
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,27 @@ Example:
1515
$templateProcessor->setValue('Name', 'Somebody someone');
1616
$templateProcessor->setValue('Street', 'Coming-Undone-Street 32');
1717
18+
You can also use ``TemplateProcessor::setValuesFromArray`` method to perform replacements from an array of "variable => value"-pairs.
19+
20+
Example:
21+
22+
.. code-block:: php
23+
24+
$replacements = [
25+
'Name' => 'Somebody someone',
26+
'Street' => 'Coming-Undone-Street 32'
27+
];
28+
$templateProcessor = new TemplateProcessor('Template.docx');
29+
$templateProcessor->setValuesFromArray($replacements);
30+
1831
It is not possible to directly add new OOXML elements to the template file being processed, but it is possible to transform main document part of the template using XSLT (see ``TemplateProcessor::applyXslStyleSheet``).
1932

2033
See ``Sample_07_TemplateCloneRow.php`` for example on how to create
2134
multirow from a single row in a template by using ``TemplateProcessor::cloneRow``.
2235

36+
See ``Sample_37_TemplateCloneRowFromArray.php`` for example on how to create
37+
multirow from a single row with a two-dimensional array as data-source in a template by using ``TemplateProcessor::cloneRowFromArray``.
38+
2339
See ``Sample_23_TemplateBlock.php`` for example on how to clone a block
2440
of text using ``TemplateProcessor::cloneBlock`` and delete a block of text using
2541
``TemplateProcessor::deleteBlock``.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
include_once 'Sample_Header.php';
3+
4+
// Template processor instance creation
5+
echo date('H:i:s'), ' Creating new TemplateProcessor instance...', EOL;
6+
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('resources/Sample_07_TemplateCloneRow.docx');
7+
8+
// Variables on different parts of document
9+
$replacements = [
10+
'weekday' => htmlspecialchars(date('l')), // On section/content
11+
'time' => htmlspecialchars(date('H:i')), // On footer
12+
'serverName' => htmlspecialchars(realpath(__DIR__)), // On header
13+
];
14+
$templateProcessor->setValuesFromArray($replacements);
15+
16+
// Simple table
17+
$rows = [
18+
[
19+
'rowNumber' => 1,
20+
'rowValue' => 'Sun'
21+
],
22+
[
23+
'rowNumber' => 2,
24+
'rowValue' => 'Mercury'
25+
],
26+
[
27+
'rowNumber' => 3,
28+
'rowValue' => 'Venus'
29+
],
30+
[
31+
'rowNumber' => 4,
32+
'rowValue' => 'Earth'
33+
],
34+
[
35+
'rowNumber' => 5,
36+
'rowValue' => 'Mars'
37+
],
38+
[
39+
'rowNumber' => 6,
40+
'rowValue' => 'Jupiter'
41+
],
42+
[
43+
'rowNumber' => 7,
44+
'rowValue' => 'Saturn'
45+
],
46+
[
47+
'rowNumber' => 8,
48+
'rowValue' => 'Uranus'
49+
],
50+
[
51+
'rowNumber' => 9,
52+
'rowValue' => 'Neptun'
53+
],
54+
[
55+
'rowNumber' => 10,
56+
'rowValue' => 'Pluto'
57+
]
58+
];
59+
$templateProcessor->cloneRowFromArray('rowValue', $rows);
60+
61+
// Table with a spanned cell
62+
$rows = [
63+
[
64+
'userId' => 1,
65+
'userFirstName' => 'James',
66+
'userName' => 'Taylor',
67+
'userPhone' => '+1 428 889 773'
68+
],
69+
[
70+
'userId' => 2,
71+
'userFirstName' => 'Robert',
72+
'userName' => 'Bell',
73+
'userPhone' => '+1 428 889 774'
74+
],
75+
[
76+
'userId' => 3,
77+
'userFirstName' => 'Michael',
78+
'userName' => 'Ray',
79+
'userPhone' => '+1 428 889 775'
80+
]
81+
];
82+
$templateProcessor->cloneRowFromArray('userId', $rows);
83+
84+
85+
echo date('H:i:s'), ' Saving the result document...', EOL;
86+
$templateProcessor->saveAs('results/Sample_07_TemplateCloneRow.docx');
87+
88+
echo getEndingNotes(array('Word2007' => 'docx'));
89+
if (!CLI) {
90+
include_once 'Sample_Footer.php';
91+
}

src/PhpWord/TemplateProcessor.php

100644100755
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ public function setValue($macro, $replace, $limit = self::MAXIMUM_REPLACEMENTS_D
155155
}
156156
}
157157

158+
/**
159+
* Set values from a one-dimensional array of "variable => value"-pairs.
160+
*
161+
* @param array $values
162+
*
163+
* @return void
164+
*/
165+
public function setValuesFromArray($values)
166+
{
167+
foreach ($values as $macro => $replace) {
168+
$this->setValue($macro, $replace);
169+
}
170+
}
171+
158172
/**
159173
* Returns array of all variables in template.
160174
*
@@ -234,6 +248,26 @@ public function cloneRow($search, $numberOfClones)
234248
$this->tempDocumentMainPart = $result;
235249
}
236250

251+
/**
252+
* Clone a table row and populates it's values from a two-dimensional array in a template document.
253+
*
254+
* @param string $search
255+
* @param array $rows
256+
*
257+
* @return void
258+
*/
259+
public function cloneRowFromArray($search, $rows)
260+
{
261+
$this->cloneRow($search, count($rows));
262+
263+
foreach ($rows as $rowKey => $rowData) {
264+
$rowNumber = $rowKey+1;
265+
foreach ($rowData as $macro => $replace) {
266+
$this->setValue($macro.'#'.$rowNumber,$replace);
267+
}
268+
}
269+
}
270+
237271
/**
238272
* Clone a block.
239273
*

0 commit comments

Comments
 (0)