Last active
September 20, 2021 20:05
-
-
Save xor-gate/73d81dcf5e0302e29c518f1db57935d1 to your computer and use it in GitHub Desktop.
QJSEngine template engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QJSEngine> | |
#include <QDebug> | |
#include <QCoreApplication> | |
#include <QFile> | |
#include <QTextStream> | |
#include <QVariantMap> | |
QString loadFile(QString fileName) | |
{ | |
QFile file(fileName); | |
if (!file.open(QIODevice::ReadOnly)) { | |
qDebug() << "Unable to open file " << fileName; | |
return QString(""); | |
} | |
QTextStream fstream(&file); | |
QString data = fstream.readAll(); | |
file.close(); | |
return data; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication app(argc, argv); | |
QJSEngine js; | |
auto data = loadFile("ejs.min.js"); | |
QJSValue result = js.evaluate(data, "ejs.min.js"); | |
if (result.isError()) { | |
qDebug() << "result: " << result.property("lineNumber").toInt() << ":" << result.toString(); | |
} | |
auto tmpl = loadFile("main.ejs"); | |
js.globalObject().setProperty("template", tmpl); | |
QString jsRender("ejs.render(template, data);"); | |
QVariantMap renderData; | |
QStringList users; | |
users << "jerry" << "steven" << "maurice"; | |
renderData.insert("users", users); | |
js.globalObject().setProperty("data", js.toScriptValue(renderData)); | |
result = js.evaluate(jsRender, "render.js"); | |
if (result.isError()) { | |
qDebug() << "result: " << result.property("lineNumber").toInt() << ":" << result.toString(); | |
} else { | |
QTextStream ts( stdout ); | |
ts << result.toString(); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% if (users) { %> | |
There are <%- users.length %> users: | |
<% users.forEach(function(user) { -%> | |
- <%= user %> | |
<% }); %> | |
JSON: <%- JSON.stringify(users) -%> | |
<% if (users.includes("jerry")) { -%> | |
There is a user named jerry! | |
<% } -%> | |
<% users.forEach(function(user) { -%> | |
<%_ switch (user) { | |
case 'steven' : -%> | |
There is a user named steven! | |
<% break; | |
} -%> | |
<% }); -%> | |
<% } -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SOURCES = main.cpp | |
QT += core qml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Download ejs.min.js from https://cdn.jsdelivr.net/npm/[email protected]/ejs.min.js | |
See also https://cpp.hotexamples.com/examples/-/QJSEngine/toScriptValue/cpp-qjsengine-toscriptvalue-method-examples.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment