Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Allow dynamic counter names in increment tag #11671

Merged
merged 1 commit into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/Tags/Increment.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,24 @@ public function reset()
return '';
}

public function wildcard($tag)
public function index()
{
if (! isset(self::$arr[$tag])) {
return self::$arr[$tag] = $this->params->get('from', 0);
$counter = $this->params->get('counter', null);

return $this->increment($counter);
}

public function wildcard($counter)
{
return $this->increment($counter);
}

protected function increment($counter)
{
if (! isset(self::$arr[$counter])) {
return self::$arr[$counter] = $this->params->get('from', 0);
}

return self::$arr[$tag] = self::$arr[$tag] + $this->params->get('by', 1);
return self::$arr[$counter] = self::$arr[$counter] + $this->params->get('by', 1);
}
}
15 changes: 15 additions & 0 deletions tests/Tags/IncrementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ class IncrementTest extends TestCase
protected $data = [
'data' => [
[
'category' => 'A',
'articles' => [
['title' => 'One'],
['title' => 'Two'],
],
],
[
'category' => 'B',
'articles' => [
['title' => 'Three'],
['title' => 'Four'],
Expand All @@ -43,6 +45,19 @@ public function basic_increment_works()
);
}

#[Test]
public function increment_with_dynamic_name_works()
{
$template = <<<'EOT'
{{ data }}{{ category }}-{{ articles }}{{ increment :counter="category" by="10" }}-{{ /articles }}{{ /data }}
EOT;

$this->assertSame(
'A-0-10-B-0-10-',
$this->tag($template, $this->data)
);
}

#[Test]
public function increment_with_starting_value_works()
{
Expand Down