48
48
import com .github .jknack .handlebars .internal .HbsParser .BoolParamContext ;
49
49
import com .github .jknack .handlebars .internal .HbsParser .CharParamContext ;
50
50
import com .github .jknack .handlebars .internal .HbsParser .CommentContext ;
51
+ import com .github .jknack .handlebars .internal .HbsParser .DynamicPathContext ;
51
52
import com .github .jknack .handlebars .internal .HbsParser .ElseBlockContext ;
52
53
import com .github .jknack .handlebars .internal .HbsParser .EscapeContext ;
53
54
import com .github .jknack .handlebars .internal .HbsParser .HashContext ;
59
60
import com .github .jknack .handlebars .internal .HbsParser .SexprContext ;
60
61
import com .github .jknack .handlebars .internal .HbsParser .SpacesContext ;
61
62
import com .github .jknack .handlebars .internal .HbsParser .StatementContext ;
63
+ import com .github .jknack .handlebars .internal .HbsParser .StaticPathContext ;
62
64
import com .github .jknack .handlebars .internal .HbsParser .StringParamContext ;
63
65
import com .github .jknack .handlebars .internal .HbsParser .SubParamExprContext ;
64
66
import com .github .jknack .handlebars .internal .HbsParser .TemplateContext ;
76
78
*/
77
79
abstract class TemplateBuilder extends HbsParserBaseVisitor <Object > {
78
80
81
+ /**
82
+ * Get partial info: static vs dynamic.
83
+ *
84
+ * @author edgar
85
+ * @since 2.2.0
86
+ */
87
+ private static class PartialInfo {
88
+
89
+ /** Token to report errors. */
90
+ private Token token ;
91
+
92
+ /** Partial params. */
93
+ private Map <String , Object > hash ;
94
+
95
+ /** Partial path: static vs subexpression. */
96
+ private Template path ;
97
+
98
+ /** Template context. */
99
+ private String context ;
100
+
101
+ }
102
+
79
103
/**
80
104
* A handlebars object. required.
81
105
*/
@@ -384,16 +408,6 @@ protected void afterApply(final Context context) {
384
408
@ Override
385
409
public Template visitPartial (final PartialContext ctx ) {
386
410
hasTag (true );
387
- Token pathToken = ctx .PATH ().getSymbol ();
388
- String uri = pathToken .getText ();
389
- if (uri .startsWith ("[" ) && uri .endsWith ("]" )) {
390
- uri = uri .substring (1 , uri .length () - 1 );
391
- }
392
-
393
- if (uri .startsWith ("/" )) {
394
- String message = "found: '/', partial shouldn't start with '/'" ;
395
- reportError (null , pathToken .getLine (), pathToken .getCharPositionInLine (), message );
396
- }
397
411
398
412
String indent = line .toString ();
399
413
if (hasTag ()) {
@@ -404,16 +418,54 @@ public Template visitPartial(final PartialContext ctx) {
404
418
indent = null ;
405
419
}
406
420
407
- TerminalNode partialContext = ctx .QID ();
421
+ PartialInfo info = (PartialInfo ) super .visit (ctx .pexpr ());
422
+
408
423
String startDelim = ctx .start .getText ();
409
- Template partial = new Partial (handlebars , uri ,
410
- partialContext != null ? partialContext .getText () : null , hash (ctx .hash ()))
424
+ Template partial = new Partial (handlebars , info .path , info .context , info .hash )
411
425
.startDelimiter (startDelim .substring (0 , startDelim .length () - 1 ))
412
426
.endDelimiter (ctx .stop .getText ())
413
427
.indent (indent )
414
428
.filename (source .filename ())
415
- .position (pathToken .getLine (), pathToken .getCharPositionInLine ());
429
+ .position (info .token .getLine (), info .token .getCharPositionInLine ());
430
+
431
+ return partial ;
432
+ }
433
+
434
+ @ Override
435
+ public PartialInfo visitStaticPath (final StaticPathContext ctx ) {
436
+ Token pathToken = ctx .path ;
437
+ String uri = pathToken .getText ();
438
+ if (uri .startsWith ("[" ) && uri .endsWith ("]" )) {
439
+ uri = uri .substring (1 , uri .length () - 1 );
440
+ }
441
+
442
+ if (uri .startsWith ("/" )) {
443
+ String message = "found: '/', partial shouldn't start with '/'" ;
444
+ reportError (null , pathToken .getLine (), pathToken .getCharPositionInLine (), message );
445
+ }
446
+
447
+ TerminalNode partialContext = ctx .QID (1 );
448
+
449
+ PartialInfo partial = new PartialInfo ();
450
+ partial .token = pathToken ;
451
+ partial .path = new Text (handlebars , uri );
452
+ partial .hash = hash (ctx .hash ());
453
+ partial .context = partialContext != null ? partialContext .getText () : null ;
454
+ return partial ;
455
+ }
456
+
457
+ @ Override
458
+ public PartialInfo visitDynamicPath (final DynamicPathContext ctx ) {
459
+ SexprContext sexpr = ctx .sexpr ();
460
+ TerminalNode qid = sexpr .QID ();
461
+ Template expression = newVar (qid .getSymbol (), TagType .SUB_EXPRESSION , params (sexpr .param ()),
462
+ hash (sexpr .hash ()), ctx .start .getText (), ctx .stop .getText ());
416
463
464
+ PartialInfo partial = new PartialInfo ();
465
+ partial .path = expression ;
466
+ partial .hash = hash (ctx .hash ());
467
+ partial .context = null ;
468
+ partial .token = qid .getSymbol ();
417
469
return partial ;
418
470
}
419
471
0 commit comments