Skip to content

Commit c1d0932

Browse files
authored
[5.x] Allow dynamic counter names in increment tag (#11671)
1 parent a39fa2f commit c1d0932

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/Tags/Increment.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,24 @@ public function reset()
3636
return '';
3737
}
3838

39-
public function wildcard($tag)
39+
public function index()
4040
{
41-
if (! isset(self::$arr[$tag])) {
42-
return self::$arr[$tag] = $this->params->get('from', 0);
41+
$counter = $this->params->get('counter', null);
42+
43+
return $this->increment($counter);
44+
}
45+
46+
public function wildcard($counter)
47+
{
48+
return $this->increment($counter);
49+
}
50+
51+
protected function increment($counter)
52+
{
53+
if (! isset(self::$arr[$counter])) {
54+
return self::$arr[$counter] = $this->params->get('from', 0);
4355
}
4456

45-
return self::$arr[$tag] = self::$arr[$tag] + $this->params->get('by', 1);
57+
return self::$arr[$counter] = self::$arr[$counter] + $this->params->get('by', 1);
4658
}
4759
}

tests/Tags/IncrementTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ class IncrementTest extends TestCase
1111
protected $data = [
1212
'data' => [
1313
[
14+
'category' => 'A',
1415
'articles' => [
1516
['title' => 'One'],
1617
['title' => 'Two'],
1718
],
1819
],
1920
[
21+
'category' => 'B',
2022
'articles' => [
2123
['title' => 'Three'],
2224
['title' => 'Four'],
@@ -43,6 +45,19 @@ public function basic_increment_works()
4345
);
4446
}
4547

48+
#[Test]
49+
public function increment_with_dynamic_name_works()
50+
{
51+
$template = <<<'EOT'
52+
{{ data }}{{ category }}-{{ articles }}{{ increment :counter="category" by="10" }}-{{ /articles }}{{ /data }}
53+
EOT;
54+
55+
$this->assertSame(
56+
'A-0-10-B-0-10-',
57+
$this->tag($template, $this->data)
58+
);
59+
}
60+
4661
#[Test]
4762
public function increment_with_starting_value_works()
4863
{

0 commit comments

Comments
 (0)