@@ -184,9 +184,7 @@ private function BlockStatement(BlockStatement $block): string
184184
185185 // Regular section: {{#"foo"}}...{{/"foo"}}
186186 $ body = $ this ->compileProgram ($ block ->program , true );
187- $ else = $ block ->inverse
188- ? ", function( \$cx, \$in) {return " . $ this ->compileProgram ($ block ->inverse ) . ";} "
189- : '' ;
187+ $ else = $ this ->compileElseClause ($ block );
190188 return "'. " . $ this ->getFuncName ('sec ' , "\$cx, $ var, null, \$in, false, function( \$cx, \$in) use (& \$sp) {return $ body;} $ else " ) . ").' " ;
191189 }
192190
@@ -195,10 +193,26 @@ private function BlockStatement(BlockStatement $block): string
195193 return $ this ->compileInvertedSection ($ block );
196194 }
197195
196+ // Non-simple path with params: invoke as a dynamic block helper call
197+ if ($ block ->params ) {
198+ return $ this ->compileDynamicBlockHelper ($ block );
199+ }
200+
198201 // Regular section: {{#var}}...{{/var}}
199202 return $ this ->compileSection ($ block );
200203 }
201204
205+ private function compileDynamicBlockHelper (BlockStatement $ block ): string
206+ {
207+ $ varPath = $ this ->compileExpression ($ block ->path );
208+ $ bp = $ block ->program ->blockParams ;
209+ $ params = $ this ->compileParams ($ block ->params , $ block ->hash , $ bp ?: null );
210+ $ body = $ this ->compileProgramWithBlockParams ($ block ->program , $ bp );
211+ $ else = $ this ->compileElseClause ($ block );
212+
213+ return "'. " . $ this ->getFuncName ('dynhbbch ' , "\$cx, $ varPath, $ params, \$in, function( \$cx, \$in) {return $ body;} $ else " ) . ").' " ;
214+ }
215+
202216 private function resolveHelper (string $ helperName ): bool
203217 {
204218 if (isset ($ this ->context ->helpers [$ helperName ])) {
@@ -253,17 +267,8 @@ private function compileEach(BlockStatement $block): string
253267 $ bp = $ block ->program ? $ block ->program ->blockParams : [];
254268 $ bs = $ bp ? Expression ::listString ($ bp ) : 'null ' ;
255269
256- if ($ bp ) {
257- array_unshift ($ this ->blockParamValues , $ bp );
258- }
259- $ body = $ block ->program ? $ this ->compileProgram ($ block ->program , true ) : "'' " ;
260- if ($ bp ) {
261- array_shift ($ this ->blockParamValues );
262- }
263-
264- $ else = $ block ->inverse
265- ? ", function( \$cx, \$in) {return " . $ this ->compileProgram ($ block ->inverse ) . ";} "
266- : '' ;
270+ $ body = $ block ->program ? $ this ->compileProgramWithBlockParams ($ block ->program , $ bp , true ) : "'' " ;
271+ $ else = $ this ->compileElseClause ($ block );
267272
268273 return "'. " . $ this ->getFuncName ('sec ' , "\$cx, LR::dv( $ var, \$in), $ bs, \$in, true, function( \$cx, \$in) use (& \$sp) {return $ body;} $ else " ) . ").' " ;
269274 }
@@ -279,9 +284,7 @@ private function compileWith(BlockStatement $block): string
279284 $ bs = $ bp ? Expression ::listString ($ bp ) : 'null ' ;
280285
281286 $ body = $ block ->program ? $ this ->compileProgram ($ block ->program ) : "'' " ;
282- $ else = $ block ->inverse
283- ? ", function( \$cx, \$in) {return " . $ this ->compileProgram ($ block ->inverse ) . ";} "
284- : '' ;
287+ $ else = $ this ->compileElseClause ($ block );
285288
286289 return "'. " . $ this ->getFuncName ('wi ' , "\$cx, LR::dv( $ var, \$in), $ bs, \$in, function( \$cx, \$in) {return $ body;} $ else " ) . ").' " ;
287290 }
@@ -291,9 +294,7 @@ private function compileSection(BlockStatement $block): string
291294 $ var = $ this ->compileExpression ($ block ->path );
292295
293296 $ body = $ block ->program ? $ this ->compileProgram ($ block ->program , true ) : "'' " ;
294- $ else = $ block ->inverse
295- ? ", function( \$cx, \$in) {return " . $ this ->compileProgram ($ block ->inverse ) . ";} "
296- : '' ;
297+ $ else = $ this ->compileElseClause ($ block );
297298
298299 return "'. " . $ this ->getFuncName ('sec ' , "\$cx, $ var, null, \$in, false, function( \$cx, \$in) use (& \$sp) {return $ body;} $ else " ) . ").' " ;
299300 }
@@ -324,16 +325,8 @@ private function compileBlockHelper(BlockStatement $block, string $helperName):
324325 return "'. " . $ this ->getFuncName ('hbbch ' , "\$cx, ' $ helperName', $ params, \$in, true, function( \$cx, \$in) {return $ body;} " ) . ").' " ;
325326 }
326327
327- if ($ bp ) {
328- array_unshift ($ this ->blockParamValues , $ bp );
329- }
330- $ body = $ this ->compileProgram ($ block ->program );
331- if ($ bp ) {
332- array_shift ($ this ->blockParamValues );
333- }
334- $ else = $ block ->inverse
335- ? ", function( \$cx, \$in) {return " . $ this ->compileProgram ($ block ->inverse ) . ";} "
336- : '' ;
328+ $ body = $ this ->compileProgramWithBlockParams ($ block ->program , $ bp );
329+ $ else = $ this ->compileElseClause ($ block );
337330
338331 return "'. " . $ this ->getFuncName ('hbbch ' , "\$cx, ' $ helperName', $ params, \$in, false, function( \$cx, \$in) {return $ body;} $ else " ) . ").' " ;
339332 }
@@ -581,16 +574,7 @@ private function PathExpression(PathExpression $expression): string
581574 $ depth = $ expression ->depth ;
582575 $ parts = $ expression ->parts ;
583576
584- $ base = $ data ? '$cx->spVars ' : '$in ' ;
585-
586- // Handle depth (../ traversal)
587- if ($ depth > 0 ) {
588- if ($ data ) {
589- $ base .= str_repeat ("['_parent'] " , $ depth );
590- } else {
591- $ base = "\$cx->scopes[count( \$cx->scopes)- $ depth] " ;
592- }
593- }
577+ $ base = $ this ->buildBasePath ($ data , $ depth );
594578
595579 // Filter out SubExpression parts for string-only operations
596580 $ stringParts = array_values (array_filter ($ parts , fn ($ p ) => is_string ($ p )));
@@ -615,28 +599,19 @@ private function PathExpression(PathExpression $expression): string
615599 if ($ bpIdx !== null ) {
616600 $ escapedName = addcslashes ($ stringParts [0 ], "' \\" );
617601 $ bpBase = "\$cx->blParam[ $ bpIdx][' $ escapedName'] " ;
618- $ remaining = '' ;
619- foreach (array_slice ($ stringParts , 1 ) as $ part ) {
620- $ remaining .= "[' " . addcslashes ($ part , "' \\" ) . "'] " ;
621- }
602+ $ remaining = $ this ->buildKeyAccess (array_slice ($ stringParts , 1 ));
622603 return "$ bpBase$ remaining ?? $ miss " ;
623604 }
624605 }
625606
626607 // Build array access string
627- $ n = '' ;
628- foreach ($ stringParts as $ part ) {
629- $ n .= "[' " . addcslashes ($ part , "' \\" ) . "'] " ;
630- }
608+ $ n = $ this ->buildKeyAccess ($ stringParts );
631609
632610 // Handle .length special case
633611 $ lastPart = end ($ stringParts );
634612 if ($ lastPart === 'length ' ) {
635613 $ varParts = array_slice ($ stringParts , 0 , -1 );
636- $ p = '' ;
637- foreach ($ varParts as $ part ) {
638- $ p .= "[' " . addcslashes ($ part , "' \\" ) . "'] " ;
639- }
614+ $ p = $ this ->buildKeyAccess ($ varParts );
640615
641616 $ checks = [];
642617 if ($ depth > 0 ) {
@@ -881,6 +856,60 @@ private function getSimpleHelperName(PathExpression|Literal $path): ?string
881856 return $ path ->parts [0 ];
882857 }
883858
859+ /**
860+ * Compile the else/inverse clause of a block as a trailing closure argument, or '' if absent.
861+ */
862+ private function compileElseClause (BlockStatement $ block ): string
863+ {
864+ return $ block ->inverse
865+ ? ", function( \$cx, \$in) {return " . $ this ->compileProgram ($ block ->inverse ) . ";} "
866+ : '' ;
867+ }
868+
869+ /**
870+ * Compile a block program, pushing/popping block params around the compilation.
871+ * @param string[] $bp
872+ */
873+ private function compileProgramWithBlockParams (Program $ program , array $ bp , bool $ withSp = false ): string
874+ {
875+ if ($ bp ) {
876+ array_unshift ($ this ->blockParamValues , $ bp );
877+ }
878+ $ body = $ this ->compileProgram ($ program , $ withSp );
879+ if ($ bp ) {
880+ array_shift ($ this ->blockParamValues );
881+ }
882+ return $ body ;
883+ }
884+
885+ /**
886+ * Build the base path expression for a given data flag and depth.
887+ */
888+ private function buildBasePath (bool $ data , int $ depth ): string
889+ {
890+ $ base = $ data ? '$cx->spVars ' : '$in ' ;
891+ if ($ depth > 0 ) {
892+ $ base = $ data
893+ ? $ base . str_repeat ("['_parent'] " , $ depth )
894+ : "\$cx->scopes[count( \$cx->scopes)- $ depth] " ;
895+ }
896+ return $ base ;
897+ }
898+
899+ /**
900+ * Build a chained array-access string for the given path parts.
901+ * e.g. ['foo', 'bar'] → "['foo']['bar']"
902+ * @param string[] $parts
903+ */
904+ private function buildKeyAccess (array $ parts ): string
905+ {
906+ $ n = '' ;
907+ foreach ($ parts as $ part ) {
908+ $ n .= "[' " . addcslashes ($ part , "' \\" ) . "'] " ;
909+ }
910+ return $ n ;
911+ }
912+
884913 /**
885914 * Build function call with optional strict-mode debug wrapping.
886915 */
@@ -950,20 +979,8 @@ private function compilePathWithLookup(PathExpression $path, string $lookupCode)
950979 $ depth = $ path ->depth ;
951980 $ parts = array_values (array_filter ($ path ->parts , fn ($ p ) => is_string ($ p )));
952981
953- $ base = $ data ? '$cx->spVars ' : '$in ' ;
954-
955- if ($ depth > 0 ) {
956- if ($ data ) {
957- $ base .= str_repeat ("['_parent'] " , $ depth );
958- } else {
959- $ base = "\$cx->scopes[count( \$cx->scopes)- $ depth] " ;
960- }
961- }
962-
963- $ n = '' ;
964- foreach ($ parts as $ part ) {
965- $ n .= "[' " . addcslashes ($ part , "' \\" ) . "'] " ;
966- }
982+ $ base = $ this ->buildBasePath ($ data , $ depth );
983+ $ n = $ this ->buildKeyAccess ($ parts );
967984
968985 $ miss = $ this ->context ->options ->strict
969986 ? "LR::miss(' " . addcslashes ($ path ->original , "' \\" ) . "') "
0 commit comments