Skip to content

Commit

Permalink
Enhance foreach tag to support dynamic variables (#4012)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni authored Aug 23, 2021
1 parent c0cc7f2 commit 23d1da2
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Tags/Iterate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ class Iterate extends Tags
{
protected static $aliases = ['foreach'];

/**
* Maps to the {{ iterate }} tag.
*
* @return mixed
*/
public function index()
{
return $this->iterate($this->params->get('array'));
}

/**
* Maps to the {{ iterate:fieldname }} tag.
*
Expand All @@ -15,10 +25,15 @@ class Iterate extends Tags
* @return mixed
*/
public function wildcard($tag)
{
return $this->iterate($this->context->get($tag));
}

protected function iterate($items)
{
[$keyKey, $valueKey] = $this->getKeyNames();

$items = collect($this->context->get($tag))
$items = collect($items)
->map(function ($value, $key) use ($keyKey, $valueKey) {
return [$keyKey => $key, $valueKey => $value];
});
Expand Down

0 comments on commit 23d1da2

Please sign in to comment.