@@ -9,40 +9,40 @@ export default (kire: Kire) => {
99 description : 'Conditionally renders a block of content if the expression is true.' ,
1010 example : `@if(user.isLoggedIn)\n Welcome, {{ user.name }}!\n@end` ,
1111 parents : [
12- {
12+ {
1313 name : 'elseif' ,
1414 params : [ 'cond:string' ] ,
1515 children : true ,
1616 type : 'js' ,
1717 description : 'Renders a block of content if the preceding @if/@elseif is false and the current expression is true.' ,
1818 example : `@elseif(user.isAdmin)\n Admin access granted.\n@end` ,
19- onCall ( c ) {
20- c . res ( `} else if (${ c . param ( 'cond' ) } ) {` ) ;
21- if ( c . children ) c . set ( c . children ) ;
22- }
19+ onCall ( c ) {
20+ c . res ( `} else if (${ c . param ( 'cond' ) } ) {` ) ;
21+ if ( c . children ) c . set ( c . children ) ;
22+ }
2323 } ,
24- {
24+ {
2525 name : 'elif' , // alias for elseif
2626 params : [ 'cond:string' ] ,
27- children : true ,
27+ children : true ,
2828 type : 'js' ,
2929 description : 'Alias for @elseif.' ,
3030 example : `@elif(user.isAdmin)\n Admin access granted.\n@end` ,
31- onCall ( c ) {
32- c . res ( `} else if (${ c . param ( 'cond' ) } ) {` ) ;
33- if ( c . children ) c . set ( c . children ) ;
34- }
31+ onCall ( c ) {
32+ c . res ( `} else if (${ c . param ( 'cond' ) } ) {` ) ;
33+ if ( c . children ) c . set ( c . children ) ;
34+ }
3535 } ,
36- {
37- name : 'else' ,
38- children : true ,
36+ {
37+ name : 'else' ,
38+ children : true ,
3939 type : 'js' ,
4040 description : 'Renders a block of content if the preceding @if/@elseif expressions are all false.' ,
4141 example : `@else\n Please log in.\n@end` ,
42- onCall ( c ) {
43- c . res ( `} else {` ) ;
44- if ( c . children ) c . set ( c . children ) ;
45- }
42+ onCall ( c ) {
43+ c . res ( `} else {` ) ;
44+ if ( c . children ) c . set ( c . children ) ;
45+ }
4646 }
4747 ] ,
4848 onCall ( ctx ) {
@@ -61,30 +61,30 @@ export default (kire: Kire) => {
6161 description : 'Iterates over an array or object, similar to a JavaScript for...of loop.' ,
6262 example : `@for(user of users)\n <p>{{ user.name }}</p>\n@end` ,
6363 onCall ( ctx ) {
64- const expr = ctx . param ( 'expr' ) ;
64+ const expr = ctx . param ( 'expr' ) ;
6565 if ( expr . includes ( ' in ' ) ) {
6666 const [ lhs , rhs ] = expr . split ( ' in ' ) ;
6767 ctx . res ( `for (const ${ lhs } of ${ rhs } ) {` ) ;
6868 } else if ( expr . includes ( ' of ' ) ) {
6969 const [ lhs , rhs ] = expr . split ( ' of ' ) ;
70- ctx . res ( `for (const ${ lhs } of ${ rhs } ) {` ) ;
70+ ctx . res ( `for (const ${ lhs } of ${ rhs } ) {` ) ;
7171 } else {
7272 ctx . res ( `for (${ expr } ) {` ) ;
7373 }
74-
74+
7575 if ( ctx . children ) ctx . set ( ctx . children ) ;
7676 ctx . res ( `}` ) ;
7777 }
7878 } ) ;
79-
79+
8080 kire . directive ( {
8181 name : 'const' ,
8282 params : [ 'expr:string' ] ,
8383 type : 'js' ,
8484 description : 'Declares a block-scoped constant, similar to JavaScript `const`.' ,
8585 example : `@const(myVar = 'hello world')` ,
8686 onCall ( ctx ) {
87- ctx . res ( `const ${ ctx . param ( 'expr' ) } ;` ) ;
87+ ctx . res ( `const ${ ctx . param ( 'expr' ) } ;` ) ;
8888 }
8989 } ) ;
9090
@@ -95,18 +95,24 @@ export default (kire: Kire) => {
9595 description : 'Declares a block-scoped local variable, similar to JavaScript `let`.' ,
9696 example : `@let(counter = 0)` ,
9797 onCall ( ctx ) {
98- ctx . res ( `let ${ ctx . param ( 'expr' ) } ;` ) ;
98+ ctx . res ( `let ${ ctx . param ( 'expr' ) } ;` ) ;
9999 }
100100 } ) ;
101-
101+
102102 kire . directive ( {
103103 name : 'code' ,
104104 children : true ,
105105 type : 'js' ,
106106 description : 'Executes a block of raw JavaScript code on the server.' ,
107107 example : `@code\n console.log('This runs during template compilation.');\n@end` ,
108108 onCall ( ctx ) {
109- if ( ctx . children ) ctx . set ( ctx . children ) ;
109+ if ( ctx . children ) {
110+ for ( const child of ctx . children ) {
111+ if ( child . type === 'text' && child . content ) {
112+ ctx . res ( child . content ) ;
113+ }
114+ }
115+ }
110116 }
111117 } ) ;
112118
@@ -118,69 +124,35 @@ export default (kire: Kire) => {
118124 description : 'Provides a control flow statement similar to a JavaScript switch block.' ,
119125 example : `@switch(value)\n @case(1) ... @end\n @default ... @end\n@end` ,
120126 parents : [
121- {
122- name : 'case' ,
123- params : [ 'val:string' ] ,
124- children : true ,
125- type : 'js' ,
126- description : 'A case clause for a @switch statement.' ,
127- example : `@case('A')\n <p>Value is A</p>\n@end` ,
128- onCall ( c ) {
129- c . res ( `case ${ c . param ( 'val' ) } : {` ) ;
130- if ( c . children ) c . set ( c . children ) ;
131- c . res ( `break; }` ) ;
132- }
133- } ,
134- {
135- name : 'default' ,
136- children : true ,
137- type : 'js' ,
138- description : 'The default clause for a @switch statement.' ,
139- example : `@default\n <p>Value is something else</p>\n@end` ,
140- onCall ( c ) {
141- c . res ( `default: {` ) ;
142- if ( c . children ) c . set ( c . children ) ;
143- c . res ( `}` ) ;
144- }
145- }
127+ {
128+ name : 'case' ,
129+ params : [ 'val:string' ] ,
130+ children : true ,
131+ type : 'js' ,
132+ description : 'A case clause for a @switch statement.' ,
133+ example : `@case('A')\n <p>Value is A</p>\n@end` ,
134+ onCall ( c ) {
135+ c . res ( `case ${ JSON . stringify ( c . param ( 'val' ) ) } : {` ) ;
136+ if ( c . children ) c . set ( c . children ) ;
137+ c . res ( `break; }` ) ;
138+ }
139+ } , {
140+ name : 'default' ,
141+ children : true ,
142+ type : 'js' ,
143+ description : 'The default clause for a @switch statement.' ,
144+ example : `@default\n <p>Value is something else</p>\n@end` ,
145+ onCall ( c ) {
146+ c . res ( `default: {` ) ;
147+ if ( c . children ) c . set ( c . children ) ;
148+ c . res ( `}` ) ;
149+ }
150+ }
146151 ] ,
147- onCall ( ctx ) {
148- ctx . res ( `switch (${ ctx . param ( 'expr' ) } ) {` ) ;
149- // Switch structure typically has case/default as parents (related nodes)
150- // Wait, in `parents` array we define SUB-directives.
151- // `case` and `default` ARE nested inside `switch`.
152- // But typically they are direct children in AST?
153- // OR they are siblings in the chain?
154- // Switch syntax: @switch (x) @case(1)...@end @default...@end @end
155- // No, usually: @switch(x) @case(1)... @break @default ... @end
156-
157- // If we use the `parents` logic (sub-directives), then:
158- // @switch (x)
159- // @case (1) ... @end
160- // @end
161-
162- // If `case` is defined as a sub-directive in `parents` of `switch`,
163- // the parser will treat it as a related node IF it is at the same level?
164- // No, `parents` in parser logic handles "chaining" (if...elseif...else).
165- // Switch cases are CHILDREN of the switch block.
166- // So they should be normal directives or handled via children traversal?
167-
168- // If `case` is a normal directive, it needs to be registered globally?
169- // Or registered only inside `switch`.
170- // But our `kire.directive` recurses `parents`.
171-
172- // If `case` is in `parents` of `switch`, it is registered.
173- // But parser only links it to `related` if it follows the pattern `@switch ... @case`.
174- // Chained style: `@switch(...) ... @end`.
175- // Inside `...`, we have `@case`.
176- // That is NOT a chain. That is nesting.
177-
178- // So `case` should just be processed as a child node.
179- // But `case` needs to be a valid directive.
180- // If we define it in `parents`, it gets registered.
181-
182- if ( ctx . children ) ctx . set ( ctx . children ) ;
183- ctx . res ( `}` ) ;
152+ async onCall ( ctx ) {
153+ ctx . res ( `switch (${ ctx . param ( 'expr' ) } ) {` ) ;
154+ if ( ctx . parents ) await ctx . set ( ctx . parents ) ;
155+ ctx . res ( `}` ) ;
184156 }
185157 } ) ;
186158} ;
0 commit comments