14
14
* Full. Partial calls and lineage hunting are supported. Handlebars does not
15
15
* support the mustache-specific syntax extensions, style modifiers and pattern
16
16
* parameters, because their use cases are addressed by the core Handlebars
17
- * feature set.
17
+ * feature set. It also does not support verbose partial syntax, because it
18
+ * seems like it can't tolerate slashes in partial names. But honestly, did you
19
+ * really want to use the verbose syntax anyway? I don't.
18
20
*
19
21
*/
20
22
21
23
"use strict" ;
22
24
23
25
var Handlebars = require ( 'handlebars' ) ;
24
26
27
+ // regexes, stored here so they're only compiled once
28
+ const findPartialsRE = / { { # ? > \s * ( [ \w -\/ . ] + ) (?: .| \s + ) * ?} } / g;
29
+ const findListItemsRE = / ( { { # ( ) ? ) ( l i s t ( I | i ) t e m s .) ( o n e | t w o | t h r e e | f o u r | f i v e | s i x | s e v e n | e i g h t | n i n e | t e n | e l e v e n | t w e l v e | t h i r t e e n | f o u r t e e n | f i f t e e n | s i x t e e n | s e v e n t e e n | e i g h t e e n | n i n e t e e n | t w e n t y ) ( ) ? } } / g;
30
+ const findAtPartialBlockRE = / { { # ? > \s * @ p a r t i a l - b l o c k \s * } } / g;
31
+
32
+ function escapeAtPartialBlock ( partialString ) {
33
+ var partial = partialString . replace ( findAtPartialBlockRE , '{{> @partial-block }}' )
34
+ return partial ;
35
+ }
36
+
25
37
var engine_handlebars = {
26
38
engine : Handlebars ,
27
39
engineName : 'handlebars' ,
@@ -31,26 +43,27 @@ var engine_handlebars = {
31
43
// style modifiers or pattern parameters (I think)
32
44
expandPartials : false ,
33
45
34
- // regexes, stored here so they're only compiled once
35
- findPartialsRE : / { { # ? > \s * ( [ \w -\/ . ] + ) (?: .| \s + ) * ?} } / g,
36
- findListItemsRE : / ( { { # ( ) ? ) ( l i s t ( I | i ) t e m s .) ( o n e | t w o | t h r e e | f o u r | f i v e | s i x | s e v e n | e i g h t | n i n e | t e n | e l e v e n | t w e l v e | t h i r t e e n | f o u r t e e n | f i f t e e n | s i x t e e n | s e v e n t e e n | e i g h t e e n | n i n e t e e n | t w e n t y ) ( ) ? } } / g,
37
-
38
46
// render it
39
47
renderPattern : function renderPattern ( pattern , data , partials ) {
40
48
if ( partials ) {
41
49
Handlebars . registerPartial ( partials ) ;
42
50
}
51
+ pattern . extendedTemplate = escapeAtPartialBlock ( pattern . extendedTemplate ) ;
43
52
var compiled = Handlebars . compile ( pattern . extendedTemplate ) ;
44
53
return compiled ( data ) ;
45
54
} ,
46
55
47
56
registerPartial : function ( pattern ) {
57
+ // register exact partial name
48
58
Handlebars . registerPartial ( pattern . patternPartial , pattern . template ) ;
59
+
60
+ // register verbose syntax? No, it seems that Handlebars can't tolerate
61
+ // slashes in partial names.
49
62
} ,
50
63
51
64
// find and return any {{> template-name }} within pattern
52
65
findPartials : function findPartials ( pattern ) {
53
- var matches = pattern . template . match ( this . findPartialsRE ) ;
66
+ var matches = pattern . template . match ( findPartialsRE ) ;
54
67
return matches ;
55
68
} ,
56
69
findPartialsWithStyleModifiers : function ( ) {
@@ -67,14 +80,14 @@ var engine_handlebars = {
67
80
return [ ] ;
68
81
} ,
69
82
findListItems : function ( pattern ) {
70
- var matches = pattern . template . match ( this . findListItemsRE ) ;
83
+ var matches = pattern . template . match ( findListItemsRE ) ;
71
84
return matches ;
72
85
} ,
73
86
74
87
// given a pattern, and a partial string, tease out the "pattern key" and
75
88
// return it.
76
89
findPartial : function ( partialString ) {
77
- var partial = partialString . replace ( this . findPartialsRE , '$1' ) ;
90
+ var partial = partialString . replace ( findPartialsRE , '$1' ) ;
78
91
return partial ;
79
92
}
80
93
} ;
0 commit comments