21
21
"use strict" ;
22
22
23
23
var _ = require ( 'underscore' ) ;
24
+
24
25
var partialRegistry = { } ;
26
+ var errorStyling = `
27
+ <style>
28
+ .plError {
29
+ background: linear-gradient(to bottom, #f1f1f1 0%,#ffffff 60%);
30
+ color: #444;
31
+ padding: 30px;
32
+ }
33
+ .plError h1 {
34
+ font-size: 16pt;
35
+ color: #733;
36
+ background: #fcfcfc;
37
+ border-bottom: 1px solid rgba(0, 0, 0, 0.05);
38
+ padding: 17px 30px;
39
+ margin: -30px -30px 0 -30px;
40
+ }
41
+ .plError dt { font-weight: bold; }
42
+ </style>
43
+ ` ;
44
+
25
45
26
46
// extend underscore with partial-ing methods and other necessary tooling
27
47
// HANDLESCORE! UNDERBARS!
@@ -32,18 +52,20 @@ function addParentContext(data, currentContext) {
32
52
33
53
_ . mixin ( {
34
54
renderNamedPartial : function ( partialKey , data , currentContext ) {
35
- return _ . renderPartial ( partialRegistry [ partialKey ] , data , currentContext ) ;
55
+ var compiledPartial = partialRegistry [ partialKey ] ;
56
+ if ( typeof compiledPartial !== 'function' ) { throw `Pattern ${ partialKey } not found.` ; }
57
+
58
+ return _ . renderPartial ( compiledPartial , data , currentContext ) ;
36
59
} ,
37
- renderPartial : function ( partial , dataIn , currentContext ) {
60
+ renderPartial : function ( compiledPartial , dataIn , currentContext ) {
38
61
var data = dataIn || { } ;
39
- var compiled ;
62
+
40
63
if ( dataIn && currentContext &&
41
64
dataIn instanceof Object && currentContext instanceof Object ) {
42
65
data = addParentContext ( data , currentContext ) ;
43
66
}
44
- compiled = _ . template ( partial ) ;
45
67
46
- return compiled ( data ) ;
68
+ return compiledPartial ( data ) ;
47
69
} ,
48
70
/* eslint-disable no-eval, no-unused-vars */
49
71
getPath : function ( pathString , currentContext , debug ) {
@@ -79,34 +101,47 @@ var engine_underscore = {
79
101
var compiled ;
80
102
81
103
try {
82
- compiled = _ . template ( pattern . extendedTemplate ) ;
104
+ compiled = partialRegistry [ pattern . patternPartial ] ;
83
105
} catch ( e ) {
84
- console . log ( `Error compiling template ${ pattern . patternName } :` , pattern . extendedTemplate ) ;
106
+ console . log ( `Error looking up underscore template ${ pattern . patternName } :` , pattern . extendedTemplate , e ) ;
85
107
}
86
108
87
109
// This try-catch is necessary because references to undefined variables
88
110
// in underscore templates are eval()ed directly as javascript, and as
89
111
// such will throw very real exceptions that will shatter the whole build
90
112
// process if we don't handle them.
91
113
try {
92
- // console.log('got here for pattern', pattern.patternName, pattern.extendedTemplate);
93
- // console.log('testing:', _.template('<%- foo %>')({foo: 'bar'}));
94
- // console.log('data:', data);
95
114
renderedHTML = compiled ( _ . extend ( data || { } , {
96
115
_allData : data ,
97
116
_partials : partials
98
117
} ) ) ;
99
118
} catch ( e ) {
100
- var errorMessage = `Error in underscore template ${ pattern . patternName } (${ pattern . relPath } ): [${ e . toString ( ) } ]` ;
119
+ var errorMessage = `Error rendering underscore pattern " ${ pattern . patternName } " (${ pattern . relPath } ): [${ e . toString ( ) } ]` ;
101
120
console . log ( errorMessage ) ;
102
- renderedHTML = `<h1>Error in underscore template ${ pattern . patternName } (${ pattern . relPath } )</h1><p>${ e . toString ( ) } </p>` ;
121
+ renderedHTML = `${ errorStyling } <div class="plError">
122
+ <h1>Error rendering underscore pattern "${ pattern . patternName } "</h1>
123
+ <dl>
124
+ <dt>Message</dt><dd>${ e . toString ( ) } </dd>
125
+ <dt>Partial name</dt><dd>${ pattern . patternName } </dd>
126
+ <dt>Template path</dt><dd>${ pattern . relPath } </dd>
127
+ </dl>
128
+ </div>
129
+ ` ;
103
130
}
104
131
105
132
return renderedHTML ;
106
133
} ,
107
134
108
135
registerPartial : function ( pattern ) {
109
- partialRegistry [ pattern . patternPartial ] = pattern . template ;
136
+ var compiled ;
137
+
138
+ try {
139
+ var templateString = pattern . extendedTemplate || pattern . template ;
140
+ compiled = _ . template ( templateString ) ;
141
+ } catch ( e ) {
142
+ console . log ( `Error compiling underscore template ${ pattern . patternName } :` , pattern . extendedTemplate , e ) ;
143
+ }
144
+ partialRegistry [ pattern . patternPartial ] = compiled ;
110
145
} ,
111
146
112
147
// find and return any {{> template-name }} within pattern
0 commit comments