Skip to content

Commit 6bd3e35

Browse files
committed
better readFile helper for templates
1 parent aa47863 commit 6bd3e35

5 files changed

Lines changed: 26 additions & 13 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ tmp/
3232
# www/
3333
www/*
3434

35+
app/stylesheets/sprite
36+
3537
# Bower stuff.
3638
bower_components/
3739

app/stylesheets/sprite/.gitkeep

Whitespace-only changes.

hmvc/markup/template/pages/article.jade

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ block variables
77
- self.title = 'Учебник — Javascript.ru';
88
- self.comments = {} // хм?
99
- self.comments.lenght = 5;
10-
- var content = readFile('/tmp/1.html');
10+
- var content = readFile('pages/my.html');
1111

1212

1313
block content

hmvc/markup/template/pages/my.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<b>Hello</b>

setup/render.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@ const log = require('javascript-log')(module);
1010
const jade = require('jade');
1111
const _ = require('lodash');
1212

13+
//log.debugOn();
1314

1415
module.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

Comments
 (0)