diff --git a/examples/docs/original/02-blocks.docx b/examples/docs/original/02-loops.docx similarity index 57% rename from examples/docs/original/02-blocks.docx rename to examples/docs/original/02-loops.docx index 1df58ea..3dfd056 100644 Binary files a/examples/docs/original/02-blocks.docx and b/examples/docs/original/02-loops.docx differ diff --git a/examples/docs/processed/02-blocks.docx b/examples/docs/processed/02-loops.docx similarity index 56% rename from examples/docs/processed/02-blocks.docx rename to examples/docs/processed/02-loops.docx index 6b9e3c7..9a331bc 100644 Binary files a/examples/docs/processed/02-blocks.docx and b/examples/docs/processed/02-loops.docx differ diff --git a/examples/scripts/02-blocks.php b/examples/scripts/02-loops.php similarity index 57% rename from examples/scripts/02-blocks.php rename to examples/scripts/02-loops.php index 4f89e4d..510b149 100644 --- a/examples/scripts/02-blocks.php +++ b/examples/scripts/02-loops.php @@ -3,20 +3,20 @@ use DocxTemplate\TemplateFactory; -$doc = new ExampleDoc("02-blocks.docx"); +$doc = new ExampleDoc("02-loops.docx"); $template = TemplateFactory::load($doc->getOriginalPath()); -$template->loop("block", [ +$template->loop("loop", [ [ - 'block_var' => "var1" + 'var' => "iteration1" ], [ - 'block_var' => "var2" + 'var' => "iteration2" ], [ - 'block_var' => "var3" + 'var' => "iteration3" ] ]); diff --git a/src/DocxTemplate/Content/Collection/LoopCollection.php b/src/DocxTemplate/Content/Collection/LoopCollection.php index 8750e27..5ad25c9 100644 --- a/src/DocxTemplate/Content/Collection/LoopCollection.php +++ b/src/DocxTemplate/Content/Collection/LoopCollection.php @@ -13,7 +13,7 @@ class LoopCollection extends ContentCollection /** * @var MarkedContent */ - private $currentItem; + private $currentIteration; /** * @param MarkedContent $content @@ -34,46 +34,36 @@ public function getBaseContent() /** * @return MarkedContent */ - public function getCurrentItem() + public function getCurrentIteration() { - return $this->currentItem; - } - - /** - * @param array $row - * @return $this - */ - public function assignRow(array $row) - { - $item = $this->itemStart(); - $item->assign($row); - $this->itemEnd(); - - return $this; + return $this->currentIteration; } /** + * @param array $values * @return MarkedContent */ - public function itemStart() + public function iterate(array $values = []) { - if (!$this->currentItem) { - $this->currentItem = clone $this->baseContent; + if ($this->currentIteration) { + $this->addContent($this->currentIteration); } - return $this->currentItem; - } - - public function itemEnd() - { - if ($this->currentItem) { - $this->addContent($this->currentItem); - $this->currentItem = null; + $this->currentIteration = clone $this->baseContent; + if ($values) { + $this->currentIteration->assign($values); } + + return $this->currentIteration; } public function finish() { + if ($this->currentIteration) { + $this->addContent($this->currentIteration); + $this->currentIteration = null; + } + $this->baseContent->getBindedTo()->assign([ $this->baseContent->getBindedMark() => $this ]); diff --git a/src/DocxTemplate/Content/MarkedContent.php b/src/DocxTemplate/Content/MarkedContent.php index c3bc8a7..97f7aa3 100644 --- a/src/DocxTemplate/Content/MarkedContent.php +++ b/src/DocxTemplate/Content/MarkedContent.php @@ -46,12 +46,11 @@ public function assign($key, $value = null) public function extractContent($name) { $content = $this->getContent(); - $uniqName = uniqid($name); - $extracted = $this->matcher->extractRange($name . "_start", $name . "_end", $uniqName, $content); + $extracted = $this->matcher->extractRange($name, $name, $name, $content); $this->setContent($content); $content = new MarkedContent($extracted, $this->matcher); - $content->bindTo($uniqName, $this); + $content->bindTo($name, $this); return $content; } @@ -66,7 +65,7 @@ public function loop($name, $rows = []) $loop = new LoopCollection($block); foreach ($rows as $row) { - $loop->assignRow($row); + $loop->iterate($row); } $loop->finish(); diff --git a/src/DocxTemplate/Matcher.php b/src/DocxTemplate/Matcher.php index cac7ad0..ce9616c 100644 --- a/src/DocxTemplate/Matcher.php +++ b/src/DocxTemplate/Matcher.php @@ -110,12 +110,15 @@ public function getMarks($text) public function extractRange($fromMark, $toMark, $placeMark, &$text) { $uniqId = uniqid(); - $text = $this->replaceMarks([ - $fromMark => "FROM_MARK_" . $uniqId, - $toMark => "TO_MARK_" . $uniqId - ], $text); + $tmpReplace = [ + $fromMark => "MARK_" . $uniqId, + ]; + if ($toMark != $fromMark) { + $tmpReplace[$toMark] = "MARK_" . $uniqId; + } + $text = $this->replaceMarks($tmpReplace, $text); - $pattern = "/FROM_MARK_{$uniqId}(.*)TO_MARK_{$uniqId}/"; + $pattern = "/MARK_{$uniqId}(.*)MARK_{$uniqId}/"; $rangeContent = ""; $text = preg_replace_callback($pattern, function ($matches) use (&$rangeContent, $placeMark) { if (isset($matches[1])) {