2121"use strict" ;
2222
2323var _ = require ( 'underscore' ) ;
24+
2425var 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+
2545
2646// extend underscore with partial-ing methods and other necessary tooling
2747// HANDLESCORE! UNDERBARS!
@@ -32,18 +52,20 @@ function addParentContext(data, currentContext) {
3252
3353_ . mixin ( {
3454 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 ) ;
3659 } ,
37- renderPartial : function ( partial , dataIn , currentContext ) {
60+ renderPartial : function ( compiledPartial , dataIn , currentContext ) {
3861 var data = dataIn || { } ;
39- var compiled ;
62+
4063 if ( dataIn && currentContext &&
4164 dataIn instanceof Object && currentContext instanceof Object ) {
4265 data = addParentContext ( data , currentContext ) ;
4366 }
44- compiled = _ . template ( partial ) ;
4567
46- return compiled ( data ) ;
68+ return compiledPartial ( data ) ;
4769 } ,
4870 /* eslint-disable no-eval, no-unused-vars */
4971 getPath : function ( pathString , currentContext , debug ) {
@@ -79,34 +101,47 @@ var engine_underscore = {
79101 var compiled ;
80102
81103 try {
82- compiled = _ . template ( pattern . extendedTemplate ) ;
104+ compiled = partialRegistry [ pattern . patternPartial ] ;
83105 } 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 ) ;
85107 }
86108
87109 // This try-catch is necessary because references to undefined variables
88110 // in underscore templates are eval()ed directly as javascript, and as
89111 // such will throw very real exceptions that will shatter the whole build
90112 // process if we don't handle them.
91113 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);
95114 renderedHTML = compiled ( _ . extend ( data || { } , {
96115 _allData : data ,
97116 _partials : partials
98117 } ) ) ;
99118 } 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 ( ) } ]` ;
101120 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+ ` ;
103130 }
104131
105132 return renderedHTML ;
106133 } ,
107134
108135 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 ;
110145 } ,
111146
112147 // find and return any {{> template-name }} within pattern
0 commit comments