Skip to content

Commit 01209dd

Browse files
committed
improve template processor documentation
1 parent 75620ca commit 01209dd

File tree

3 files changed

+127
-20
lines changed

3 files changed

+127
-20
lines changed

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
# built documents.
4949
#
5050
# The short X.Y version.
51-
version = '0.14.0'
51+
version = '0.16.0'
5252
# The full version, including alpha/beta/rc tags.
5353
release = version
5454

docs/containers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ You can pass an optional parameter to specify where the header/footer should be
107107
To change the evenAndOddHeaders use the ``getSettings`` method to return the Settings object, and then call the ``setEvenAndOddHeaders`` method:
108108

109109
.. code-block:: php
110+
110111
$phpWord->getSettings()->setEvenAndOddHeaders(true);
111-
112112
113113
Footers
114114
-------

docs/templates-processing.rst

Lines changed: 125 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,45 @@ Templates processing
44
====================
55

66
You can create an OOXML document template with included search-patterns (macros) which can be replaced by any value you wish. Only single-line values can be replaced.
7+
Macros are defined like this: ``${search-pattern}``.
8+
To load a template file, create a new instance of the TemplateProcessor.
79

8-
To deal with a template file, use ``new TemplateProcessor`` statement. After TemplateProcessor instance creation the document template is copied into the temporary directory. Then you can use ``TemplateProcessor::setValue`` method to change the value of a search pattern. The search-pattern model is: ``${search-pattern}``.
10+
.. code-block:: php
11+
12+
$templateProcessor = new TemplateProcessor('Template.docx');
13+
14+
setValue
15+
""""""""
16+
Given a template containing
17+
18+
.. code-block:: clean
19+
20+
Hello ${name}!
921
22+
The following will replace ``${name}`` with ``World``. The resulting document will now contain ``Hello World!``
23+
24+
.. code-block:: php
25+
26+
$templateProcessor->setValue('name', 'World');
27+
28+
setImageValue
29+
"""""""""""""
1030
The search-pattern model for images can be like:
11-
- ``${search-image-pattern}``
12-
- ``${search-image-pattern:[width]:[height]:[ratio]}``
13-
- ``${search-image-pattern:[width]x[height]}``
14-
- ``${search-image-pattern:size=[width]x[height]}``
15-
- ``${search-image-pattern:width=[width]:height=[height]:ratio=false}``
31+
- ``${search-image-pattern}``
32+
- ``${search-image-pattern:[width]:[height]:[ratio]}``
33+
- ``${search-image-pattern:[width]x[height]}``
34+
- ``${search-image-pattern:size=[width]x[height]}``
35+
- ``${search-image-pattern:width=[width]:height=[height]:ratio=false}``
36+
1637
Where:
17-
- [width] and [height] can be just numbers or numbers with measure, which supported by Word (cm|mm|in|pt|pc|px|%|em|ex)
18-
- [ratio] uses only for ``false``, ``-`` or ``f`` to turn off respect aspect ration of image. By default template image size uses as 'container' size.
38+
- [width] and [height] can be just numbers or numbers with measure, which supported by Word (cm, mm, in, pt, pc, px, %, em, ex)
39+
- [ratio] uses only for ``false``, ``-`` or ``f`` to turn off respect aspect ration of image. By default template image size uses as 'container' size.
1940

2041
Example:
2142

22-
.. code-block:: doc
43+
.. code-block:: clean
2344
24-
${CompanyLogo}
45+
${CompanyLogo}
2546
${UserLogo:50:50} ${Name} - ${City} - ${Street}
2647
2748
.. code-block:: php
@@ -30,14 +51,100 @@ Example:
3051
$templateProcessor->setValue('Name', 'John Doe');
3152
$templateProcessor->setValue(array('City', 'Street'), array('Detroit', '12th Street'));
3253
33-
$templateProcessor->setImageValue('CompanyLogo', 'path/to/company/logo.png');
34-
$templateProcessor->setImageValue('UserLogo', array('path' => 'path/to/logo.png', 'width' => 100, 'height' => 100, 'ratio' => false));
54+
$templateProcessor->setImageValue('CompanyLogo', 'path/to/company/logo.png');
55+
$templateProcessor->setImageValue('UserLogo', array('path' => 'path/to/logo.png', 'width' => 100, 'height' => 100, 'ratio' => false));
56+
57+
cloneBlock
58+
""""""""""
59+
Given a template containing
60+
See ``Sample_23_TemplateBlock.php`` for an example.
61+
62+
.. code-block:: clean
63+
64+
${block_name}
65+
Customer: ${customer_name}
66+
Address: ${customer_address}
67+
${/block_name}
68+
69+
The following will duplicate everything between ``${block_name}`` and ``${/block_name}`` 3 times.
70+
71+
.. code-block:: php
72+
73+
$templateProcessor->cloneBlock('block_name', 3, true, true);
74+
75+
The last parameter will rename any macro defined inside the block and add #1, #2, #3 ... to the macro name.
76+
The result will be
77+
78+
.. code-block:: clean
79+
80+
Customer: ${customer_name#1}
81+
Address: ${customer_address#1}
82+
Customer: ${customer_name#2}
83+
Address: ${customer_address#2}
84+
Customer: ${customer_name#3}
85+
Address: ${customer_address#3}
3586
36-
It is not possible to directly add new OOXML elements to the template file being processed, but it is possible to transform headers, main document part, and footers of the template using XSLT (see ``TemplateProcessor::applyXslStyleSheet``).
87+
replaceBlock
88+
""""""""""""
89+
Given a template containing
3790

38-
See ``Sample_07_TemplateCloneRow.php`` for example on how to create
39-
multirow from a single row in a template by using ``TemplateProcessor::cloneRow``.
91+
.. code-block:: clean
92+
93+
${block_name}
94+
This block content will be replaced
95+
${/block_name}
96+
97+
The following will replace everything between``${block_name}`` and ``${/block_name}`` with the value passed.
98+
99+
.. code-block:: php
100+
101+
$templateProcessor->replaceBlock('block_name', 'This is the replacement text.');
102+
103+
deleteBlock
104+
"""""""""""
105+
Same as previous, but it deletes the block
106+
107+
.. code-block:: php
108+
109+
$templateProcessor->deleteBlock('block_name');
110+
111+
cloneRow
112+
""""""""
113+
Clones a table row in a template document.
114+
See ``Sample_07_TemplateCloneRow.php`` for an example.
115+
116+
.. code-block:: clean
117+
118+
------------------------------
119+
| ${userId} | ${userName} |
120+
| |----------------|
121+
| | ${userAddress} |
122+
------------------------------
123+
124+
.. code-block:: php
125+
126+
$templateProcessor->cloneRow('userId', 2);
127+
128+
Will result in
129+
130+
.. code-block:: clean
131+
132+
----------------------------------
133+
| ${userId#1} | ${userName#1} |
134+
| |------------------|
135+
| | ${userAddress#1} |
136+
---------------------------------|
137+
| ${userId#2} | ${userName#2} |
138+
| |------------------|
139+
| | ${userAddress#2} |
140+
----------------------------------
141+
142+
applyXslStyleSheet
143+
""""""""""""""""""
144+
Applies the XSL stylesheet passed to header part, footer part and main part
145+
146+
.. code-block:: php
40147
41-
See ``Sample_23_TemplateBlock.php`` for example on how to clone a block
42-
of text using ``TemplateProcessor::cloneBlock`` and delete a block of text using
43-
``TemplateProcessor::deleteBlock``.
148+
$xslDomDocument = new \DOMDocument();
149+
$xslDomDocument->load('/path/to/my/stylesheet.xsl');
150+
$templateProcessor->applyXslStyleSheet($xslDomDocument);

0 commit comments

Comments
 (0)