@@ -254,20 +254,34 @@ protected function parse(string $template, array $data = [], ?array $options = n
254254 // it can potentially modify any template between its tags.
255255 $ template = $ this ->parsePlugins ($ template );
256256
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.
259263 foreach ($ data as $ key => $ val ) {
260264 $ escape = true ;
261265
262266 if (is_array ($ val )) {
263267 $ escape = false ;
264- $ replace = $ this ->parsePair ($ key , $ val , $ template );
268+ array_push ( $ replacePairsStack , [ ' replace ' => $ this ->parsePair ($ key , $ val , $ template), ' escape ' => $ escape ] );
265269 } else {
266- $ replace = $ this ->parseSingle ($ key , (string ) $ val );
270+ array_push ( $ replaceSingleStack , [ ' replace ' => $ this ->parseSingle ($ key , (string ) $ val), ' escape ' => $ escape ] );
267271 }
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 ) {
268281
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 ' ]);
271285 }
272286 }
273287
0 commit comments