include_once 'Sample_Header.php';
// Template processor instance creation
echo date('H:i:s'), ' Creating new TemplateProcessor instance...', EOL;
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('resources/Sample_23_TemplateBlock.docx');
// Will clone everything between ${tag} and ${/tag}, the number of times. By default, 1.
$templateProcessor->cloneBlock('CLONEME', 3);
// Everything between ${tag} and ${/tag}, will be deleted/erased.
$templateProcessor->deleteBlock('DELETEME');
echo date('H:i:s'), ' Saving the result document...', EOL;
$templateProcessor->saveAs('results/Sample_23_TemplateBlock.docx');
echo getEndingNotes(array('Word2007' => 'docx'));
if (!CLI) {
include_once 'Sample_Footer.php';
// Template processor instance creation
echo date('H:i:s'), ' Creating new TemplateProcessor instance...', EOL;
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('resources/Sample_07_TemplateCloneRow.docx');
$block = $templateProcessor->getTemplateStructure();
echo date('H:i:s'), ' Done getting template structure...', EOL;
// Variables on different parts of document
$templateProcessor->setValue('weekday', htmlspecialchars(date('l')));
// On section/content
$templateProcessor->setValue('time', htmlspecialchars(date('H:i')));
// On footer
$templateProcessor->setValue('serverName', htmlspecialchars(realpath(__DIR__)));
// On header
// Simple table
$templateProcessor->cloneRow('rowValue', 10);
// clone block
$templateProcessor->cloneBlock('blockToday', 2);
$templateProcessor->deleteBlock('deleteBlock');
$templateProcessor->setValue('rowValue#1', htmlspecialchars('Sun'));
$templateProcessor->setValue('rowValue#2', htmlspecialchars('Mercury'));
$templateProcessor->setValue('rowValue#3', htmlspecialchars('Venus'));
$templateProcessor->setValue('rowValue#4', htmlspecialchars('Earth'));
$templateProcessor->setValue('rowValue#5', htmlspecialchars('Mars'));
$templateProcessor->setValue('rowValue#6', htmlspecialchars('Jupiter'));
$templateProcessor->setValue('rowValue#7', htmlspecialchars('Saturn'));
$templateProcessor->setValue('rowValue#8', htmlspecialchars('Uranus'));
$templateProcessor->setValue('rowValue#9', htmlspecialchars('Neptun'));
$templateProcessor->setValue('rowValue#10', htmlspecialchars('Pluto'));
$templateProcessor->setValue('rowNumber#1', htmlspecialchars('1'));
$templateProcessor->setValue('rowNumber#2', htmlspecialchars('2'));
$templateProcessor->setValue('rowNumber#3', htmlspecialchars('3'));
$templateProcessor->setValue('rowNumber#4', htmlspecialchars('4'));
function mergeDOCX($source_file, $merged_file)
// Important: we get the merge data first, because the phpWord
// autoloader included below stuffs up the Jethro autoloader
// and causes errors.
$data = array_values($this->getMergeData());
// NB THIS FILE HAS BEEN CHANGED!
require_once 'include/phpword/src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register();
\PhpOffice\PhpWord\Settings::setTempDir(dirname($source_file));
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($source_file);
if (!$templateProcessor->cloneBlock('MERGEBLOCK', count($data))) {
$vars = $templateProcessor->getVariables();
if (empty($vars)) {
trigger_error("You don't seem to have included any \${keywords} in your file; cannot merge");
return;
$templateProcessor->cloneRow(reset($vars), count($data));
foreach ($data as $num => $row) {
foreach ($row as $k => $v) {
$templateProcessor->setValue(strtoupper($k) . '#' . ($num + 1), $this->xmlEntities($v));
$templateProcessor->saveAs($merged_file);