Skip to content

Commit a1bc04c

Browse files
committed
fix: parsing equal key name replace for parser
1 parent b33b418 commit a1bc04c

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

system/View/Parser.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,29 +256,28 @@ protected function parse(string $template, array $data = [], ?array $options = n
256256

257257
// Parse stack for each parse type (Single and Pairs)
258258
$replaceSingleStack = [];
259-
$replacePairsStack = [];
259+
$replacePairsStack = [];
260260

261261
// loop over the data variables, saving regex and data
262262
// for later replacement.
263263
foreach ($data as $key => $val) {
264264
$escape = true;
265265

266266
if (is_array($val)) {
267-
$escape = false;
268-
array_push($replacePairsStack, ['replace' => $this->parsePair($key, $val, $template), 'escape' => $escape]);
267+
$escape = false;
268+
$replacePairsStack[] = ['replace' => $this->parsePair($key, $val, $template), 'escape' => $escape];
269269
} else {
270-
array_push($replaceSingleStack, ['replace' => $this->parseSingle($key, (string) $val), 'escape' => $escape]);
270+
$replaceSingleStack[] = ['replace' => $this->parseSingle($key, (string) $val), 'escape' => $escape];
271271
}
272272
}
273273

274274
// Merge both stacks, pairs first + single stacks
275275
// This allows for nested data with the same key to be replaced properly
276276
$replace = array_merge($replacePairsStack, $replaceSingleStack);
277277

278-
// Loop over each replace array item which
278+
// Loop over each replace array item which
279279
// holds all the data to be replaced
280280
foreach ($replace as $replaceItem) {
281-
282281
// Loop over the actual data to be replaced
283282
foreach ($replaceItem['replace'] as $pattern => $content) {
284283
$template = $this->replaceSingle($pattern, $content, $template, $replaceItem['escape']);

tests/system/View/ParserTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,4 +1061,44 @@ public function testChangeConditionalDelimitersWorkWithJavaScriptCode(): void
10611061
EOL;
10621062
$this->assertSame($expected, $this->parser->renderString($template));
10631063
}
1064+
1065+
/**
1066+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/9245
1067+
*/
1068+
public function testParseSameArrayKeyName(): void
1069+
{
1070+
$data = [
1071+
'type' => 'Super Powers',
1072+
'powers' => [
1073+
[
1074+
'type' => 'invisibility',
1075+
],
1076+
],
1077+
];
1078+
1079+
$template = '{type} like {powers}{type}{/powers}';
1080+
1081+
$this->parser->setData($data);
1082+
$this->assertSame('Super Powers like invisibility', $this->parser->renderString($template));
1083+
}
1084+
1085+
public function testParseSameArrayKeyNameNested(): void
1086+
{
1087+
$data = [
1088+
'title' => 'My title',
1089+
'similar' => [
1090+
['items' => [
1091+
[
1092+
'title' => 'My similar title',
1093+
],
1094+
],
1095+
],
1096+
],
1097+
];
1098+
1099+
$template = '{title} with similar item {similar}{items}{title}{/items}{/similar}';
1100+
1101+
$this->parser->setData($data);
1102+
$this->assertSame('My title with similar item My similar title', $this->parser->renderString($template));
1103+
}
10641104
}

0 commit comments

Comments
 (0)