From c5ec097dba41807a5a0fbbf81f3388872fc96d13 Mon Sep 17 00:00:00 2001 From: welefen Date: Wed, 30 Oct 2013 17:22:56 +0800 Subject: [PATCH] by welefen --- App/Conf/config.js | 3 +- App/Lib/Action/Home/IndexAction.class.js | 9 ++-- lib/Common/functions.js | 14 +++---- lib/Conf/convention.js | 14 +++---- lib/Conf/tags.js | 4 +- lib/Lib/Behavior/CheckRouteBehavior.class.js | 5 +-- lib/Lib/Behavior/DenyIpBehavior.class.js | 5 +-- .../LocationTemplateBehavior.class.js | 41 +++++++++++++++++++ .../Behavior/ParseTemplateBehavior.class.js | 18 ++++++++ lib/Lib/Core/App.class.js | 2 +- lib/Lib/Core/Think.class.js | 4 ++ lib/Lib/Core/View.class.js | 27 +++++++++++- lib/Lib/Driver/Template/EjsTemplate.class.js | 11 +++++ lib/Lib/Driver/Template/JadeTemplate.class.js | 13 ++++++ .../Driver/Template/NsmartyTemplate.class.js | 10 +++++ 15 files changed, 148 insertions(+), 32 deletions(-) create mode 100644 lib/Lib/Behavior/LocationTemplateBehavior.class.js create mode 100644 lib/Lib/Behavior/ParseTemplateBehavior.class.js create mode 100644 lib/Lib/Driver/Template/EjsTemplate.class.js create mode 100644 lib/Lib/Driver/Template/JadeTemplate.class.js create mode 100644 lib/Lib/Driver/Template/NsmartyTemplate.class.js diff --git a/App/Conf/config.js b/App/Conf/config.js index d180857e..aa470da4 100644 --- a/App/Conf/config.js +++ b/App/Conf/config.js @@ -1,4 +1,5 @@ module.exports = { //"配置项": "配置值" - deny_ip: [] + deny_ip: [], + tpl_engine_type: "" }; \ No newline at end of file diff --git a/App/Lib/Action/Home/IndexAction.class.js b/App/Lib/Action/Home/IndexAction.class.js index 21538b7a..634c06d9 100644 --- a/App/Lib/Action/Home/IndexAction.class.js +++ b/App/Lib/Action/Home/IndexAction.class.js @@ -5,12 +5,11 @@ module.exports = Action(function(){ }, indexAction: function(){ var cookie = this.cookie("name"); - this.header("Access-Control-Allow-Origin", "*"); - console.log(__request.headers); - //this.cookie("name", "welefen test"); - this.echo("welefen"); - this.end(); //this.redirect("http://www.baidu.com"); + this.assign({ + name: "welefen" + }) + this.display(); }, testAction: function(test, test2){ console.log("test value: " + test + ","+test2); diff --git a/lib/Common/functions.js b/lib/Common/functions.js index 0fa85726..6efc9120 100644 --- a/lib/Common/functions.js +++ b/lib/Common/functions.js @@ -87,12 +87,12 @@ global.Behavior = function(obj){ * 调用一个指定的行为 * @param {[type]} name [description] */ -global.B = function(name){ +global.B = function(name, data){ var cls = name + "Behavior"; if (APP_DEBUG) { G('behavior_start'); }; - var result = think_require(cls)().run(); + var result = think_require(cls)().run(data); if (APP_DEBUG) { G('behavior_end'); }; @@ -103,10 +103,10 @@ global.B = function(name){ * 处理标签扩展 * @return {[type]} [description] */ -global.tag = function(name){ +global.tag = function(name, data){ var sys_tags = C('sys_tags.' + name); var tags = C('tags.' + name); - global.__behavior_value = undefined; + global.__behavior_data = data; if (tags) { var override = false; if (typeof tags[0] == 'boolean') { @@ -124,13 +124,13 @@ global.tag = function(name){ G(name + '_start'); }; tags.forEach(function(b){ - var result = B(b); + var result = B(b, __behavior_data); if (result !== undefined) { - __behavior_value = result; + __behavior_data = result; }; }) }; - return __behavior_value; + return __behavior_data; } /** * 记录时间和内存使用情况 diff --git a/lib/Conf/convention.js b/lib/Conf/convention.js index 5de8fc01..19d47841 100644 --- a/lib/Conf/convention.js +++ b/lib/Conf/convention.js @@ -19,8 +19,8 @@ module.exports = { url_module_map: {}, //模块别名,隐藏真实模块名 url_action_map: {}, //Action别名 app_group_mode: 0, //0为普通分组,1为独立分组 - call_method: "__call", //当找不到方法时调用什么方法 - url_params_bind: true, //方法参数绑定 + call_method: "__call", //当找不到方法时调用什么方法,这个方法存在时才有效 + url_params_bind: true, //方法参数绑定,将URL参数值绑定到action的参数上 class_file_suffix: ".class.js", //类文件后缀 action_suffix: "Action", //action后缀 cookie_expires: 0, //cookie默认保持时间数,默认随浏览器关闭失效 @@ -29,12 +29,10 @@ module.exports = { tpl_content_type: "text/html", //模版输出类型 tpl_file_suffix: ".html", //模版文件名后缀 tpl_file_depr: "_", //module和action之间的分隔符 - - tpl_engine_type: "nsmarty", //模版引擎名称 + + tpl_engine_type: "ejs", //模版引擎名称 tpl_engine_config: { //模版引擎需要的配置 - left_delimiter: "{=", - right_delimiter: "=}", - auto_literal: false, - force_compile: false + open: "{%", + end: "%}", } }; \ No newline at end of file diff --git a/lib/Conf/tags.js b/lib/Conf/tags.js index 9586dfef..01665f12 100644 --- a/lib/Conf/tags.js +++ b/lib/Conf/tags.js @@ -11,8 +11,8 @@ module.exports = { action_begin: [], action_end: [], view_begin: [], - view_template: [], - view_parse: [], + view_template: ["LocationTemplate"], + view_parse: ["ParseTemplate"], view_filter: [], view_end: [] } \ No newline at end of file diff --git a/lib/Lib/Behavior/CheckRouteBehavior.class.js b/lib/Lib/Behavior/CheckRouteBehavior.class.js index 404be7e8..8f68fda1 100644 --- a/lib/Lib/Behavior/CheckRouteBehavior.class.js +++ b/lib/Lib/Behavior/CheckRouteBehavior.class.js @@ -5,7 +5,7 @@ */ var url = require("url"); var Dispatcher = think_require("Dispatcher"); -var behavior = Behavior(function(){ +var behavior = module.exports = Behavior(function(){ return { options: { 'url_route_on': false, //是否开启自定义URL路由 @@ -166,5 +166,4 @@ var behavior = Behavior(function(){ return reg; } } -}); -module.exports = behavior; \ No newline at end of file +}); \ No newline at end of file diff --git a/lib/Lib/Behavior/DenyIpBehavior.class.js b/lib/Lib/Behavior/DenyIpBehavior.class.js index 0002ed32..3c42e0b8 100644 --- a/lib/Lib/Behavior/DenyIpBehavior.class.js +++ b/lib/Lib/Behavior/DenyIpBehavior.class.js @@ -2,7 +2,7 @@ * 阻止ip来源访问 * @return {[type]} [description] */ -var behavior = Behavior(function(){ +var behavior = module.exports = Behavior(function(){ return { options: { deny_ip: [] @@ -29,5 +29,4 @@ var behavior = Behavior(function(){ return true; } } -}); -module.exports = behavior; \ No newline at end of file +}); \ No newline at end of file diff --git a/lib/Lib/Behavior/LocationTemplateBehavior.class.js b/lib/Lib/Behavior/LocationTemplateBehavior.class.js new file mode 100644 index 00000000..614bbf36 --- /dev/null +++ b/lib/Lib/Behavior/LocationTemplateBehavior.class.js @@ -0,0 +1,41 @@ +/** + * 定位模版的行为 + * @return {[type]} [description] + */ +var behavior = module.exports = Behavior(function(){ + return { + run: function(templateFile){ + if (!is_file(templateFile)) { + return this.parseTemplateFile(templateFile); + }; + }, + parseTemplateFile: function(templateFile){ + templateFile = templateFile || ""; + if (!templateFile) { + templateFile = [ + TMPL_PATH, "/", __http.req.group, "/", + __http.req.module.toLowerCase(), + C('tpl_file_depr'), + __http.req.action.toLowerCase(), + C('tpl_file_suffix') + ].join(""); + }else if(templateFile.indexOf(C('tpl_file_suffix')) === -1){ + var path = templateFile.split(":"); + var action = path.pop(); + var module = path.pop() || __http.req.module.toLowerCase(); + var group = ucfirst(path.pop()) || __http.req.group; + templateFile = [ + TMPL_PATH, "/", group, "/", + module, + C('tpl_file_depr'), + action, + C('tpl_file_suffix') + ].join(""); + } + if (!is_file(templateFile)) { + throw_error(templateFile + " is not exist"); + }; + return templateFile; + } + } +}); \ No newline at end of file diff --git a/lib/Lib/Behavior/ParseTemplateBehavior.class.js b/lib/Lib/Behavior/ParseTemplateBehavior.class.js new file mode 100644 index 00000000..3c4cc0c8 --- /dev/null +++ b/lib/Lib/Behavior/ParseTemplateBehavior.class.js @@ -0,0 +1,18 @@ +/** + * 调用对应的模版引擎解析模版 + * @return {[type]} [description] + */ +var behavior = module.exports = Behavior(function(){ + return { + run: function(data){ + var content = data.content || data.file; + var engine = C('tpl_engine_type'); + //不使用模版引擎 + if (!engine) { + return file_get_contents(content); + }; + var engineClass = ucfirst(engine) + "Template"; + return think_require(engineClass).fetch(content, data.var); + } + } +}); \ No newline at end of file diff --git a/lib/Lib/Core/App.class.js b/lib/Lib/Core/App.class.js index 43abbe22..5975a2c4 100644 --- a/lib/Lib/Core/App.class.js +++ b/lib/Lib/Core/App.class.js @@ -44,7 +44,7 @@ var App = { }; }; if (!/^[A-Za-z](\w)*$/.test(action)) { - throw_error('action error'); + throw_error('action name error'); }; if (typeof module[action] == 'function') { if (C('url_params_bind')) { diff --git a/lib/Lib/Core/Think.class.js b/lib/Lib/Core/Think.class.js index 2d98fcd7..f5f95d69 100644 --- a/lib/Lib/Core/Think.class.js +++ b/lib/Lib/Core/Think.class.js @@ -88,6 +88,10 @@ var Think = { EXTEND_PATH + "/Driver/Cache/" + sysfile, THINK_LIB_PATH + "/Driver/Cache/" + sysfile ], + Template: [ + EXTEND_PATH + "/Driver/Template/" + sysfile, + THINK_LIB_PATH + "/Driver/Template/" + sysfile + ] }; for(var name in config){ var length = name.length; diff --git a/lib/Lib/Core/View.class.js b/lib/Lib/Core/View.class.js index ea977917..abf7a607 100644 --- a/lib/Lib/Core/View.class.js +++ b/lib/Lib/Core/View.class.js @@ -17,16 +17,39 @@ var view = module.exports = Class(function(){ }; return this.tVar[name]; }, - display: function(templateFile, charset, conentType, content, prefix){ + display: function(templateFile, charset, contentType, content, prefix){ tag("view_begin"); content = this.fetch(templateFile, content, prefix); this.render(content, charset, contentType); tag("view_end"); + __response.end(); }, - render: function(conent, charset, contentType){ + render: function(content, charset, contentType){ if (charset === undefined) { charset = C('encoding'); }; + if (contentType === undefined) { + contentType = C('tpl_content_type'); + }; + __http.res.setHeader("Content-Type", contentType + "; charset="+charset); + __http.res.setHeader("X-Powered-By", "thinkjs"); + __response.write(content || "", C('encoding')); + }, + fetch: function(templateFile, content, prefix){ + if (content === undefined) { + templateFile = tag("view_template", templateFile); + if (!is_file(templateFile)) { + return ""; + }; + }; + content = tag("view_parse", { + "var": this.tVar, + "file": templateFile, + "content": content, + "prefix": prefix + }); + content = tag("view_filter", content); + return content; } } }) \ No newline at end of file diff --git a/lib/Lib/Driver/Template/EjsTemplate.class.js b/lib/Lib/Driver/Template/EjsTemplate.class.js new file mode 100644 index 00000000..4fa327c0 --- /dev/null +++ b/lib/Lib/Driver/Template/EjsTemplate.class.js @@ -0,0 +1,11 @@ +/** + * ejs + * https://github.com/visionmedia/ejs + * @type {[type]} + */ +var ejs = require("ejs"); +var template = module.exports = { + fetch: function(templateFile, tVar){ + + } +} \ No newline at end of file diff --git a/lib/Lib/Driver/Template/JadeTemplate.class.js b/lib/Lib/Driver/Template/JadeTemplate.class.js new file mode 100644 index 00000000..e6022102 --- /dev/null +++ b/lib/Lib/Driver/Template/JadeTemplate.class.js @@ -0,0 +1,13 @@ +/** + * jade + * @type {[type]} + */ +var jade = require("jade"); +var template = module.exports = { + fetch: function(templateFile, tVar){ + var content = file_get_contents(templateFile); + var fn = jade.compile(content, {}); + var html = fn(tVar); + return html; + } +} \ No newline at end of file diff --git a/lib/Lib/Driver/Template/NsmartyTemplate.class.js b/lib/Lib/Driver/Template/NsmartyTemplate.class.js new file mode 100644 index 00000000..03024d2e --- /dev/null +++ b/lib/Lib/Driver/Template/NsmartyTemplate.class.js @@ -0,0 +1,10 @@ +/** + * nsmarty模版 + * @type {[type]} + */ +var nsmarty = require("nsmarty"); +var template = module.exports = { + fetch: function(templateFile, tVar){ + + } +} \ No newline at end of file