After a lot of testing the only template system that was small and gave decent error messages was Nunjucks. I'll go with that.

main
Zed A. Shaw 9 months ago
parent 7284e510d8
commit 7421ff513d
  1. 14
      bin/app.js
  2. 44
      package-lock.json
  3. 2
      package.json
  4. 10
      templates/todo.html

@ -3,7 +3,7 @@ import FastifyStatic from "@fastify/static";
import path from "path"; import path from "path";
import { ToDo } from "../lib/models.js"; import { ToDo } from "../lib/models.js";
import fs from "fs"; import fs from "fs";
import template from "lodash/template.js"; import nunjucks from "nunjucks";
const fastify = Fastify({ const fastify = Fastify({
logger: true logger: true
@ -14,12 +14,8 @@ fastify.get("/todo", async (req, rep) => {
const todo_list = await ToDo.all({}); const todo_list = await ToDo.all({});
// this is for exporing the problem of getting good error messages from templates // this is for exporing the problem of getting good error messages from templates
const tmpl_src = fs.readFileSync("templates/todo.html"); const result = nunjucks.render("templates/todo.html",
const tmpl = template(tmpl_src, {sourceURL: "templates/todo.html"}); {todo_list, your_todos: "Your Todos"});
console.log("FUNCTION-----\n", tmpl.toString());
const result = tmpl({todo_list});
rep.code(200) rep.code(200)
.type("text/html") .type("text/html")
@ -31,10 +27,6 @@ fastify.get("/todo", async (req, rep) => {
} }
}); });
fastify.get("/todo.html", async (req, rep) => {
});
fastify.register(FastifyStatic, { fastify.register(FastifyStatic, {
root: path.join(path.resolve("."), 'static'), root: path.join(path.resolve("."), 'static'),
prefix: '/', // optional: default '/' prefix: '/', // optional: default '/'

44
package-lock.json generated

@ -14,7 +14,7 @@
"fastify": "^4.26.0", "fastify": "^4.26.0",
"knex": "^3.1.0", "knex": "^3.1.0",
"knex-paginate": "^3.1.1", "knex-paginate": "^3.1.1",
"lodash": "^4.17.21", "nunjucks": "^3.2.4",
"sqlite3": "^5.1.7" "sqlite3": "^5.1.7"
}, },
"bin": { "bin": {
@ -209,6 +209,11 @@
"node": ">= 6" "node": ">= 6"
} }
}, },
"node_modules/a-sync-waterfall": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/a-sync-waterfall/-/a-sync-waterfall-1.0.1.tgz",
"integrity": "sha512-RYTOHHdWipFUliRFMCS4X2Yn2X8M87V/OpSqWzKKOGhzqyUxzyVmhHDH9sAvG+ZuQf/TAOFsLCpMw09I1ufUnA=="
},
"node_modules/abbrev": { "node_modules/abbrev": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
@ -359,6 +364,11 @@
"node": ">= 6" "node": ">= 6"
} }
}, },
"node_modules/asap": {
"version": "2.0.6",
"resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz",
"integrity": "sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA=="
},
"node_modules/atomic-sleep": { "node_modules/atomic-sleep": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/atomic-sleep/-/atomic-sleep-1.0.0.tgz", "resolved": "https://registry.npmjs.org/atomic-sleep/-/atomic-sleep-1.0.0.tgz",
@ -1914,6 +1924,38 @@
"node": "^12.13.0 || ^14.15.0 || >=16.0.0" "node": "^12.13.0 || ^14.15.0 || >=16.0.0"
} }
}, },
"node_modules/nunjucks": {
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/nunjucks/-/nunjucks-3.2.4.tgz",
"integrity": "sha512-26XRV6BhkgK0VOxfbU5cQI+ICFUtMLixv1noZn1tGU38kQH5A5nmmbk/O45xdyBhD1esk47nKrY0mvQpZIhRjQ==",
"dependencies": {
"a-sync-waterfall": "^1.0.0",
"asap": "^2.0.3",
"commander": "^5.1.0"
},
"bin": {
"nunjucks-precompile": "bin/precompile"
},
"engines": {
"node": ">= 6.9.0"
},
"peerDependencies": {
"chokidar": "^3.3.0"
},
"peerDependenciesMeta": {
"chokidar": {
"optional": true
}
}
},
"node_modules/nunjucks/node_modules/commander": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-5.1.0.tgz",
"integrity": "sha512-P0CysNDQ7rtVw4QIQtm+MRxV66vKFSvlsQvGYXZWR3qFU0jlMKHZZZgw8e+8DSah4UDKMqnknRDQz+xuQXQ/Zg==",
"engines": {
"node": ">= 6"
}
},
"node_modules/on-exit-leak-free": { "node_modules/on-exit-leak-free": {
"version": "2.1.2", "version": "2.1.2",
"resolved": "https://registry.npmjs.org/on-exit-leak-free/-/on-exit-leak-free-2.1.2.tgz", "resolved": "https://registry.npmjs.org/on-exit-leak-free/-/on-exit-leak-free-2.1.2.tgz",

@ -14,7 +14,7 @@
"fastify": "^4.26.0", "fastify": "^4.26.0",
"knex": "^3.1.0", "knex": "^3.1.0",
"knex-paginate": "^3.1.1", "knex-paginate": "^3.1.1",
"lodash": "^4.17.21", "nunjucks": "^3.2.4",
"sqlite3": "^5.1.7" "sqlite3": "^5.1.7"
} }
} }

@ -15,12 +15,14 @@
</head> </head>
<body> <body>
<h1><%= your_todos %></h1> <h1>Your TODOs</h1>
<ol> <ol>
<% for(let todo of todo_list) { %> {% for todo in todo_list %}
<li><%= todo.task %></li> <li>{{ todo.task }}</li>
<% } %> {% else %}
<li>No TODO Items</li>
{% endfor %}
</ol> </ol>
</body> </body>
</html> </html>

Loading…
Cancel
Save