jj.js 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/view.js +22 -12
  2. package/package.json +1 -1
package/lib/view.js CHANGED
@@ -14,10 +14,7 @@ class View extends Context
14
14
 
15
15
  // 加载文件
16
16
  async load(template) {
17
- const file_name = this.parsePath(template);
18
- if(!isFileSync(file_name)) {
19
- throw new Error(`文件不存在(${file_name})`);
20
- }
17
+ const file_name = this.parseTplName(template);
21
18
  return await readFile(file_name);
22
19
  }
23
20
 
@@ -28,10 +25,7 @@ class View extends Context
28
25
 
29
26
  // 渲染文件
30
27
  async fetch(template) {
31
- const file_name = this.parsePath(template);
32
- if(!isFileSync(file_name)) {
33
- throw new Error(`文件不存在(${file_name})`);
34
- }
28
+ const file_name = this.parseTplName(template);
35
29
  return await this._engine(file_name, this._data || {});
36
30
  }
37
31
 
@@ -52,20 +46,36 @@ class View extends Context
52
46
  }
53
47
 
54
48
  //解析模板地址
55
- parsePath(template=this.ctx.ACTION) {
49
+ parsePath(template=this.ctx.ACTION, view_folder, view_depr) {
50
+ !view_folder && (view_folder = cfg_view.view_folder);
51
+ !view_depr && (view_depr = cfg_view.view_depr);
56
52
  let view_file = template;
53
+
57
54
  if(view_file.indexOf('/') !== 0) {
58
55
  const temp = view_file.replace(/^\/|\/$/g, '').split('/').reverse().map(u => toLine(u));
59
- const folder = this._folder;
60
- const depr = this._depr;
61
56
  if(temp.length <= 3) {
62
- view_file = `${temp[2] || this.ctx.APP}/${folder || cfg_view.view_folder}/${temp[1] || this.ctx.CONTROLLER}${depr || cfg_view.view_depr}${temp[0]}`;
57
+ view_file = `${temp[2] || this.ctx.APP}/${view_folder}/${temp[1] || this.ctx.CONTROLLER}${view_depr}${temp[0]}`;
63
58
  }
64
59
  }
65
60
  path.extname(view_file) || (view_file += cfg_view.view_ext);
66
61
  return path.join(this.$config.app.base_dir, view_file);
67
62
  }
68
63
 
64
+ //解析模板名字
65
+ parseTplName(template) {
66
+ let file_name = this.parsePath(template, this._folder, this._depr);
67
+ let is_file = isFileSync(file_name);
68
+ if(!is_file && (this._folder || this._depr)) {
69
+ file_name = this.parsePath(template);
70
+ is_file = isFileSync(file_name);
71
+ }
72
+ if(!is_file) {
73
+ file_name = this.parsePath(template, this._folder, this._depr);
74
+ throw new Error(`文件不存在(${file_name})`);
75
+ }
76
+ return file_name;
77
+ }
78
+
69
79
  // 设置模版引擎
70
80
  setEngine(engine) {
71
81
  this._engine = typeof engine == 'string' ? require(engine) : engine;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jj.js",
3
- "version": "0.8.1",
3
+ "version": "0.8.2",
4
4
  "description": "A simple and lightweight MVC framework built on nodejs+koa2",
5
5
  "main": "jj.js",
6
6
  "scripts": {