@@ -17,13 +17,6 @@ function testTemplate(string $title, array $templates, string $exp = '')
1717}
1818
1919
20- Assert::exception (function () {
21- testTemplate ('unexpected content ' , [
22- 'main ' => '{embed "embed.latte"} {$a} {/embed} ' ,
23- ]);
24- }, Latte \CompileException::class, 'Unexpected content inside {embed} tags (on line 1 at column 23) ' );
25-
26-
2720testTemplate ('keyword file ' , [
2821 'main ' => '{embed file embed}{/embed} ' ,
2922 'embed ' => 'embed ' ,
@@ -919,6 +912,144 @@ testTemplate(
919912);
920913
921914
915+ testTemplate (
916+ 'default block: caller content overrides the fallback ' ,
917+ [
918+ 'main ' => <<<'XX'
919+ {embed "embed.latte"}custom body{/embed}
920+ XX,
921+ 'embed.latte ' => <<<'XX'
922+ start
923+ {block default}fallback body{/block}
924+ end
925+ XX,
926+ ],
927+ <<<'XX'
928+ start
929+ custom body
930+ end
931+ XX,
932+ );
933+
934+
935+ testTemplate (
936+ 'default block: empty caller keeps the fallback ' ,
937+ [
938+ 'main ' => <<<'XX'
939+ {embed "embed.latte"}{/embed}
940+ XX,
941+ 'embed.latte ' => <<<'XX'
942+ start
943+ {block default}fallback body{/block}
944+ end
945+ XX,
946+ ],
947+ <<<'XX'
948+ start
949+ fallback body
950+ end
951+ XX,
952+ );
953+
954+
955+ testTemplate (
956+ 'default block: self-closing caller keeps the fallback ' ,
957+ [
958+ 'main ' => <<<'XX'
959+ {embed "embed.latte"/}
960+ XX,
961+ 'embed.latte ' => <<<'XX'
962+ start
963+ {block default}fallback body{/block}
964+ end
965+ XX,
966+ ],
967+ <<<'XX'
968+ start
969+ fallback body
970+ end
971+ XX,
972+ );
973+
974+
975+ testTemplate (
976+ 'default block: whitespace-only caller keeps the fallback ' ,
977+ [
978+ 'main ' => <<<'XX'
979+ {embed "embed.latte"} {/embed}
980+ XX,
981+ 'embed.latte ' => <<<'XX'
982+ start
983+ {block default}fallback body{/block}
984+ end
985+ XX,
986+ ],
987+ <<<'XX'
988+ start
989+ fallback body
990+ end
991+ XX,
992+ );
993+
994+
995+ testTemplate (
996+ 'default block: default content sees caller variables ' ,
997+ [
998+ 'main ' => <<<'XX'
999+ {var $greeting = 'Hello'}{embed "embed.latte"}{var $name = 'world'}{$greeting} {$name}{if true}!{/if}{/embed}
1000+ XX,
1001+ 'embed.latte ' => <<<'XX'
1002+ start
1003+ {block default}fallback body{/block}
1004+ end
1005+ XX,
1006+ ],
1007+ <<<'XX'
1008+ start
1009+ Hello world!
1010+ end
1011+ XX,
1012+ );
1013+
1014+
1015+ testTemplate (
1016+ 'default block: works together with named blocks ' ,
1017+ [
1018+ 'main ' => <<<'XX'
1019+ {embed "embed.latte"}custom body{block title}custom title{/block}{/embed}
1020+ XX,
1021+ 'embed.latte ' => <<<'XX'
1022+ title={block title}fallback title{/block}
1023+ body={block default}fallback body{/block}
1024+ XX,
1025+ ],
1026+ <<<'XX'
1027+ title=custom title
1028+ body=custom body
1029+ XX,
1030+ );
1031+
1032+
1033+ testTemplate (
1034+ 'default block: caller content ignored without a placeholder ' ,
1035+ [
1036+ 'main ' => <<<'XX'
1037+ {embed "embed.latte"}custom body{/embed}
1038+ XX,
1039+ 'embed.latte ' => 'no placeholder here ' ,
1040+ ],
1041+ 'no placeholder here ' ,
1042+ );
1043+
1044+
1045+ Assert::exception (function () {
1046+ testTemplate ('default block: loose content mixed with explicit block ' , [
1047+ 'main ' => '{embed "embed.latte"}custom body{block default}explicit body{/block}{/embed} ' ,
1048+ 'embed.latte ' => '[{block default}x{/block}] ' ,
1049+ ]);
1050+ }, Latte \CompileException::class, 'Cannot combine loose content with an explicit {block default} inside {embed}; both define the default block (on line 1 at column 22) ' );
1051+
1052+
9221053$ latte = createLatte ();
9231054Assert::exception (
9241055 fn () => $ latte ->renderToString ('{embed (null)/} ' ),
0 commit comments