@@ -10,16 +10,14 @@ const log = require('javascript-log')(module);
1010const jade = require ( 'jade' ) ;
1111const _ = require ( 'lodash' ) ;
1212
13+ //log.debugOn();
1314
1415module . exports = function render ( app ) {
1516 app . use ( function * ( next ) {
1617
1718 this . locals = { } ;
1819
1920 this . locals . moment = moment ;
20- this . locals . readFile = function ( file ) {
21- return fs . readFileSync ( file ) ;
22- } ;
2321
2422 // render(__dirname, 'article', {}) -- 3 args
2523 // render(__dirname, 'article') -- 2 args
@@ -58,13 +56,26 @@ module.exports = function render(app) {
5856 return templatePath ;
5957 }
6058
61- return resolvePathUp ( templateDir , templatePath ) ;
59+ return resolvePathUp ( templateDir , templatePath + '.jade' ) ;
6260 } ;
6361
64- var loc = _ . assign ( { parser : JadeParser } , config . template . options , this . locals , locals ) ;
62+ var serviceLocals = {
63+ parser : JadeParser ,
64+ readFile : function ( file ) {
65+ if ( file [ 0 ] == '.' ) {
66+ throw new Error ( "readFile file must not start with . : bad file " + file ) ;
67+ }
68+ var path = resolvePathUp ( templateDir , file ) ;
69+ if ( ! path ) {
70+ throw new Error ( "Not found " + file + " (from dir " + templateDir + ")" ) ;
71+ }
72+ return fs . readFileSync ( path ) ;
73+ }
74+ } ;
75+ var loc = _ . assign ( serviceLocals , config . template . options , this . locals , locals ) ;
6576
6677// console.log(loc);
67- var file = resolvePathUp ( templateDir , templatePath ) ;
78+ var file = resolvePathUp ( templateDir , templatePath + '.jade' ) ;
6879 if ( ! file ) {
6980 throw new Error ( "Template file not found: " + templatePath + " (in dir " + templateDir + ") " ) ;
7081 }
@@ -83,17 +94,16 @@ function resolvePathUp(templateDir, templateName) {
8394 var top = path . resolve ( process . cwd ( ) ) ;
8495
8596 while ( templateDir != path . dirname ( top ) ) {
86- var template = path . join ( templateDir , 'template' , templateName + '.jade' ) ;
87- // log.debug("-- try path", template);
97+ var template = path . join ( templateDir , 'template' , templateName ) ;
98+ log . debug ( "-- try path" , template ) ;
8899 if ( fs . existsSync ( template ) ) {
89- // log.debug("-- found");
100+ log . debug ( "-- found" ) ;
90101 return template ;
91102 }
92- // log.debug("-- skipped");
103+ log . debug ( "-- skipped" ) ;
93104 templateDir = path . dirname ( templateDir ) ;
94105 }
95106
107+ log . debug ( "-- failed" , templateRoot , top ) ;
96108 return null ;
97-
98- // log.debug("-- end", templateRoot, top);
99109}
0 commit comments