diff --git a/README.md b/README.md index 000856b..f1e4f84 100644 --- a/README.md +++ b/README.md @@ -37,4 +37,4 @@ JSTE is a NodeJS-Module for rendering data in static HTML ``` ## special constants: -- ```_TEMPLATE_``` -> Defines the path to the temp file \ No newline at end of file +- ```_TEMPLATE_``` -> Defines the path to the temp file \ No newline at end of file diff --git a/index.js b/index.js index eefaacd..fe53f3c 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,11 @@ - -var engine = require("./src/modules/engine") +var engine = require("./src/modules/engine"); var app = {}; app.config = { + templatePath: __dirname + "\\src\\templates", + pagePath: __dirname + "\\src\\pages", +}; - "templatePath":__dirname+"\\src\\templates", - "pagePath":__dirname+"\\src\\pages" - - }; - -app.render = engine.render(pagecode,templatecode); +app.render = engine.render(pagecode, templatecode); // app.getTemplateNameFromPage = function(pageName) // { @@ -16,4 +13,3 @@ app.render = engine.render(pagecode,templatecode); // } module.exports = app; - diff --git a/src/modules/engine.js b/src/modules/engine.js index 2aa2cab..03a6442 100644 --- a/src/modules/engine.js +++ b/src/modules/engine.js @@ -1,42 +1,37 @@ const fs = require("fs"); const path = require("path"); -var appdir = path.join(__dirname, '..'); -var app = {} +var appdir = path.join(__dirname, ".."); +var app = {}; function escapeRegExp(string) { - return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); + return string.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"); } function replaceAll(str, find, replace) { - return str.replace(new RegExp(escapeRegExp(find), 'g'), replace); + return str.replace(new RegExp(escapeRegExp(find), "g"), replace); } -app.render = function(pagecode, templatecode) { - result = ""; +app.render = function (pagecode, templatecode) { + result = ""; - if(!pagecode == JSON) - pagecode = JSON.parse(pagecode); + if (!pagecode == JSON) pagecode = JSON.parse(pagecode); - //TODO - if(templatecode === null || templatecode == undefined) - { - templatecode == fs.readFileSync(pagecode["_TEMPLATE_"]+".html"); - } + //TODO + if (templatecode === null || templatecode == undefined) { + templatecode == fs.readFileSync(pagecode["_TEMPLATE_"] + ".html"); + } + for (var i in pagecode) { + var value = undefined; - for (var i in pagecode) { - var value = undefined; + var re = new RegExp(/\d*_([A-Z])\w*_/g); + if (re.test(i)) continue; - var re = new RegExp(/\d*_([A-Z])\w*_/g); - if(re.test(i)) - continue; + value = pagecode[i].toString(); + templatecode = replaceAll(templatecode, "<[" + i + "]>", value); + } - value = pagecode[i].toString(); - templatecode = replaceAll(templatecode,"<["+i+"]>",value); - } + return templatecode.replace(new RegExp(/\d*<\[([A-Z])\w*\]>/g), ""); +}; - return templatecode.replace(new RegExp(/\d*<\[([A-Z])\w*\]>/g),"");; -} - - -module.exports = app; \ No newline at end of file +module.exports = app;