Skip to content

Commit

Permalink
loops example
Browse files Browse the repository at this point in the history
  • Loading branch information
tyz910 committed Mar 21, 2015
1 parent cbcaa38 commit 8fc39af
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 41 deletions.
Binary file not shown.
Binary file not shown.
10 changes: 5 additions & 5 deletions examples/scripts/02-blocks.php → examples/scripts/02-loops.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
]);

Expand Down
44 changes: 17 additions & 27 deletions src/DocxTemplate/Content/Collection/LoopCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class LoopCollection extends ContentCollection
/**
* @var MarkedContent
*/
private $currentItem;
private $currentIteration;

/**
* @param MarkedContent $content
Expand All @@ -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
]);
Expand Down
7 changes: 3 additions & 4 deletions src/DocxTemplate/Content/MarkedContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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();
Expand Down
13 changes: 8 additions & 5 deletions src/DocxTemplate/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down

0 comments on commit 8fc39af

Please sign in to comment.