Skip to content

Commit 7790b6a

Browse files
committed
Pass values to replace macros with in cloneBlock
1 parent 01209dd commit 7790b6a

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ v0.16.0 (xx dec 2018)
1313
- Add support for setting images in TemplateProcessor @SailorMax #1170
1414
- Add "Plain Text" type to SDT (Structured Document Tags) @morrisdj #1541
1515
- Added possibility to index variables inside cloned block in TemplateProcessor @JPBetley #817
16+
- Added possibility to replace variables inside cloned block with values in TemplateProcessor @DIDoS #1392
1617

1718
### Fixed
1819
- Fix regex in `cloneBlock` function @nicoder #1269

docs/templates-processing.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,34 @@ The result will be
7979
8080
Customer: ${customer_name#1}
8181
Address: ${customer_address#1}
82+
8283
Customer: ${customer_name#2}
8384
Address: ${customer_address#2}
85+
8486
Customer: ${customer_name#3}
8587
Address: ${customer_address#3}
8688
89+
It is also possible to pass an array with the values to replace the marcros with.
90+
If an array with replacements is passed, the ``count`` argument is ignored, it is the size of the array that counts.
91+
92+
.. code-block:: php
93+
94+
$replacements = array(
95+
array('customer_name' => 'Batman', 'customer_address' => 'Gotham City'),
96+
array('customer_name' => 'Superman', 'customer_address' => 'Metropolis'),
97+
);
98+
$templateProcessor->cloneBlock('block_name', 0, true, false, $replacements);
99+
100+
The result will then be
101+
102+
.. code-block:: clean
103+
104+
Customer: Batman
105+
Address: Gotham City
106+
107+
Customer: Superman
108+
Address: Metropolis
109+
87110
replaceBlock
88111
""""""""""""
89112
Given a template containing

src/PhpWord/TemplateProcessor.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,10 +632,11 @@ public function cloneRow($search, $numberOfClones)
632632
* @param int $clones How many time the block should be cloned
633633
* @param bool $replace
634634
* @param bool $indexVariables If true, any variables inside the block will be indexed (postfixed with #1, #2, ...)
635+
* @param array $variableReplacements Array containing replacements for macros found inside the block to clone
635636
*
636637
* @return string|null
637638
*/
638-
public function cloneBlock($blockname, $clones = 1, $replace = true, $indexVariables = false)
639+
public function cloneBlock($blockname, $clones = 1, $replace = true, $indexVariables = false, $variableReplacements = null)
639640
{
640641
$xmlBlock = null;
641642
preg_match(
@@ -648,6 +649,8 @@ public function cloneBlock($blockname, $clones = 1, $replace = true, $indexVaria
648649
$xmlBlock = $matches[3];
649650
if ($indexVariables) {
650651
$cloned = $this->indexClonedVariables($clones, $xmlBlock);
652+
} elseif ($variableReplacements !== null && is_array($variableReplacements)) {
653+
$cloned = $this->replaceClonedVariables($variableReplacements, $xmlBlock);
651654
} else {
652655
$cloned = array();
653656
for ($i = 1; $i <= $clones; $i++) {
@@ -960,4 +963,26 @@ protected function indexClonedVariables($count, $xmlBlock)
960963

961964
return $results;
962965
}
966+
967+
/**
968+
* Raplaces variables with values from array, array keys are the variable names
969+
*
970+
* @param array $variableReplacements
971+
* @param string $xmlBlock
972+
*
973+
* @return string[]
974+
*/
975+
protected function replaceClonedVariables($variableReplacements, $xmlBlock)
976+
{
977+
$results = array();
978+
foreach ($variableReplacements as $replacementArray) {
979+
$localXmlBlock = $xmlBlock;
980+
foreach ($replacementArray as $search => $replacement) {
981+
$localXmlBlock = $this->setValueForPart(self::ensureMacroCompleted($search), $replacement, $localXmlBlock, self::MAXIMUM_REPLACEMENTS_DEFAULT);
982+
}
983+
$results[] = $localXmlBlock;
984+
}
985+
986+
return $results;
987+
}
963988
}

tests/PhpWord/TemplateProcessorTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,39 @@ public function testCloneBlockWithVariables()
456456
$this->assertContains('Address ${address#3}, Street ${street#3}', $templateProcessor->getMainPart());
457457
}
458458

459+
public function testCloneBlockWithVariableReplacements()
460+
{
461+
$mainPart = '<?xml version="1.0" encoding="UTF-8"?>
462+
<w:p>
463+
<w:r>
464+
<w:rPr></w:rPr>
465+
<w:t>${CLONEME}</w:t>
466+
</w:r>
467+
</w:p>
468+
<w:p>
469+
<w:r>
470+
<w:t xml:space="preserve">City: ${city}, Street: ${street}</w:t>
471+
</w:r>
472+
</w:p>
473+
<w:p>
474+
<w:r w:rsidRPr="00204FED">
475+
<w:t>${/CLONEME}</w:t>
476+
</w:r>
477+
</w:p>';
478+
479+
$replacements = array(
480+
array('city' => 'London', 'street' => 'Baker Street'),
481+
array('city' => 'New York', 'street' => '5th Avenue'),
482+
array('city' => 'Rome', 'street' => 'Via della Conciliazione'),
483+
);
484+
$templateProcessor = new TestableTemplateProcesor($mainPart);
485+
$templateProcessor->cloneBlock('CLONEME', 0, true, false, $replacements);
486+
487+
$this->assertContains('City: London, Street: Baker Street', $templateProcessor->getMainPart());
488+
$this->assertContains('City: New York, Street: 5th Avenue', $templateProcessor->getMainPart());
489+
$this->assertContains('City: Rome, Street: Via della Conciliazione', $templateProcessor->getMainPart());
490+
}
491+
459492
/**
460493
* Template macros can be fixed.
461494
*

0 commit comments

Comments
 (0)