@@ -1104,6 +1104,25 @@ public static function partialProvider(): array
11041104 'expected ' => 'This is Lucky, it is 5 years old. ' ,
11051105 ],
11061106
1107+ [
1108+ 'desc ' => 'loadPartial: hasPartial returns false before registration ' ,
1109+ 'template ' => '{{check}} {{> (loadPartial partialName)}} {{check}} ' ,
1110+ 'data ' => ['partialName ' => 'greet ' , 'name ' => 'World ' ],
1111+ 'helpers ' => [
1112+ 'check ' => function (HelperOptions $ options ): string {
1113+ return $ options ->hasPartial ('greet ' ) ? 'found ' : 'missing ' ;
1114+ },
1115+ 'loadPartial ' => function (string $ name , HelperOptions $ options ): string {
1116+ if (!$ options ->hasPartial ($ name )) {
1117+ $ partial = 'Hello {{name}} ' ;
1118+ $ options ->registerPartial ($ name , Handlebars::compile ($ partial ));
1119+ }
1120+ return $ name ;
1121+ },
1122+ ],
1123+ 'expected ' => 'missing Hello World found ' ,
1124+ ],
1125+
11071126 [
11081127 'desc ' => 'LNC#241 - each block inside inline block context ' ,
11091128 'template ' => '{{#>foo}}{{#*inline "bar"}}GOOD!{{#each .}}>{{.}}{{/each}}{{/inline}}{{/foo}} ' ,
@@ -2188,4 +2207,42 @@ public static function subexpressionPathProvider(): array
21882207 ],
21892208 ];
21902209 }
2210+
2211+ public function testLoadPartialPersistsAcrossFnCalls (): void
2212+ {
2213+ // registerPartial() writes to the persistent $cx->partials array, which fn() does not
2214+ // reset, so each template is compiled and registered only once even when a block helper
2215+ // calls fn() per iteration.
2216+ $ compileCounts = ['a ' => 0 , 'b ' => 0 ];
2217+
2218+ $ helpers = [
2219+ 'repeat ' => function (array $ items , HelperOptions $ options ): string {
2220+ $ ret = '' ;
2221+ foreach ($ items as $ item ) {
2222+ $ ret .= $ options ->fn ($ item );
2223+ }
2224+ return $ ret ;
2225+ },
2226+ 'loadPartial ' => function (string $ name , HelperOptions $ options ) use (&$ compileCounts ): string {
2227+ if (!$ options ->hasPartial ($ name )) {
2228+ $ templates = ['a ' => '[A:{{val}}] ' , 'b ' => '[B:{{val}}] ' ];
2229+ $ options ->registerPartial ($ name , Handlebars::compile ($ templates [$ name ]));
2230+ $ compileCounts [$ name ]++;
2231+ }
2232+ return $ name ;
2233+ },
2234+ ];
2235+
2236+ $ template = Handlebars::compile ('{{#repeat items}}{{> (loadPartial type)}}{{/repeat}} ' );
2237+ $ items = [
2238+ ['type ' => 'a ' , 'val ' => 1 ],
2239+ ['type ' => 'b ' , 'val ' => 2 ],
2240+ ['type ' => 'a ' , 'val ' => 3 ],
2241+ ];
2242+ $ result = $ template (['items ' => $ items ], ['helpers ' => $ helpers ]);
2243+
2244+ $ this ->assertEquals ('[A:1][B:2][A:3] ' , $ result );
2245+ $ this ->assertEquals (1 , $ compileCounts ['a ' ]);
2246+ $ this ->assertEquals (1 , $ compileCounts ['b ' ]);
2247+ }
21912248}
0 commit comments