You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
7
9
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}!
9
21
22
+
The following will replace ``${name}`` with ``World``. The resulting document will now contain ``Hello World!``
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}
35
86
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
37
90
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
40
147
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
0 commit comments