pull request

This commit is contained in:
lucaspalomodevelop 2021-05-23 16:14:19 +02:00
commit 9fd0db350f
3 changed files with 80 additions and 27 deletions

View File

@ -2,3 +2,39 @@
# JSTE
JSTE is a NodeJS-Module for rendering data in static HTML
## Example Page (JSON)
```json
{
"_TEMPLATE_":"firstexample",
"TITLE": "app2",
"LINK":"http://www.google.de"
}
```
## Example Template (HTML)
```html
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><[TITLE]></title>
</head>
<body>
<div id="app">
<a id="googlelink"><[LINK]></a>
</div>
<[[ARRAY]]>
</body>
</html>
```
## special constants:
- ```_TEMPLATE_``` -> Defines the path to the temp file

View File

@ -1,8 +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",
};
<<<<<<< HEAD
"templatePath":__dirname+"\\src\\templates",
"pagePath":__dirname+"\\src\\pages"
@ -15,7 +18,13 @@ app.render = engine.render;
// return app.pages.get(pageName)["_TEMPLATE_"]
// }
=======
app.render = engine.render;
>>>>>>> 15070407cefad5bcf866cedb53b9e41d47333d9b
// app.getTemplateNameFromPage = function(pageName)
// {
// return app.pages.get(pageName)["_TEMPLATE_"]
// }
module.exports = app;

View File

@ -1,29 +1,37 @@
const fs = require("fs");
const path = require("path");
var appdir = path.join(__dirname, '..');
var app = {}
var appdir = path.join(__dirname, "..");
var app = {};
app.render = function(pagecode, templatecode) {
result = "";
pagecode = JSON.parse(pagecode);
//TODO
//if(templatecode === null)
for (var i in pagecode) {
var value = undefined;
if(i.startsWith("_"))
continue;
value = pagecode[i].toString();
templatecode = templatecode.replace("<["+i+"]>",value)
}
return templatecode;
function escapeRegExp(string) {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&");
}
function replaceAll(str, find, replace) {
return str.replace(new RegExp(escapeRegExp(find), "g"), replace);
}
module.exports = app;
app.render = function (pagecode, templatecode) {
result = "";
if (!pagecode == JSON) pagecode = JSON.parse(pagecode);
//TODO
if (templatecode === null || templatecode == undefined) {
templatecode == fs.readFileSync(pagecode["_TEMPLATE_"] + ".html");
}
for (var i in pagecode) {
var value = undefined;
var re = new RegExp(/\d*_([A-Z])\w*_/g);
if (re.test(i)) continue;
value = pagecode[i].toString();
templatecode = replaceAll(templatecode, "<[" + i + "]>", value);
}
return templatecode.replace(new RegExp(/\d*<\[([A-Z])\w*\]>/g), "");
};
module.exports = app;