@@ -254,20 +254,34 @@ protected function parse(string $template, array $data = [], ?array $options = n
254
254
// it can potentially modify any template between its tags.
255
255
$ template = $ this ->parsePlugins ($ template );
256
256
257
- // loop over the data variables, replacing
258
- // the content as we go.
257
+ // Parse stack for each parse type (Single and Pairs)
258
+ $ replaceSingleStack = [];
259
+ $ replacePairsStack = [];
260
+
261
+ // loop over the data variables, saving regex and data
262
+ // for later replacement.
259
263
foreach ($ data as $ key => $ val ) {
260
264
$ escape = true ;
261
265
262
266
if (is_array ($ val )) {
263
267
$ escape = false ;
264
- $ replace = $ this ->parsePair ($ key , $ val , $ template );
268
+ array_push ( $ replacePairsStack , [ ' replace ' => $ this ->parsePair ($ key , $ val , $ template), ' escape ' => $ escape ] );
265
269
} else {
266
- $ replace = $ this ->parseSingle ($ key , (string ) $ val );
270
+ array_push ( $ replaceSingleStack , [ ' replace ' => $ this ->parseSingle ($ key , (string ) $ val), ' escape ' => $ escape ] );
267
271
}
272
+ }
273
+
274
+ // Merge both stacks, pairs first + single stacks
275
+ // This allows for nested data with the same key to be replaced properly
276
+ $ replace = array_merge ($ replacePairsStack , $ replaceSingleStack );
277
+
278
+ // Loop over each replace array item which
279
+ // holds all the data to be replaced
280
+ foreach ($ replace as $ replaceItem ) {
268
281
269
- foreach ($ replace as $ pattern => $ content ) {
270
- $ template = $ this ->replaceSingle ($ pattern , $ content , $ template , $ escape );
282
+ // Loop over the actual data to be replaced
283
+ foreach ($ replaceItem ['replace ' ] as $ pattern => $ content ) {
284
+ $ template = $ this ->replaceSingle ($ pattern , $ content , $ template , $ replaceItem ['escape ' ]);
271
285
}
272
286
}
273
287
0 commit comments