diff --git a/bin/app.js b/bin/app.js index 7e2d78c..598bab7 100644 --- a/bin/app.js +++ b/bin/app.js @@ -3,7 +3,7 @@ import FastifyStatic from "@fastify/static"; import path from "path"; import { ToDo } from "../lib/models.js"; import fs from "fs"; -import template from "lodash/template.js"; +import nunjucks from "nunjucks"; const fastify = Fastify({ logger: true @@ -14,12 +14,8 @@ fastify.get("/todo", async (req, rep) => { const todo_list = await ToDo.all({}); // this is for exporing the problem of getting good error messages from templates - const tmpl_src = fs.readFileSync("templates/todo.html"); - const tmpl = template(tmpl_src, {sourceURL: "templates/todo.html"}); - - console.log("FUNCTION-----\n", tmpl.toString()); - - const result = tmpl({todo_list}); + const result = nunjucks.render("templates/todo.html", + {todo_list, your_todos: "Your Todos"}); rep.code(200) .type("text/html") @@ -31,10 +27,6 @@ fastify.get("/todo", async (req, rep) => { } }); -fastify.get("/todo.html", async (req, rep) => { - -}); - fastify.register(FastifyStatic, { root: path.join(path.resolve("."), 'static'), prefix: '/', // optional: default '/' diff --git a/package-lock.json b/package-lock.json index a548caf..01e660a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,7 @@ "fastify": "^4.26.0", "knex": "^3.1.0", "knex-paginate": "^3.1.1", - "lodash": "^4.17.21", + "nunjucks": "^3.2.4", "sqlite3": "^5.1.7" }, "bin": { @@ -209,6 +209,11 @@ "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": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", @@ -359,6 +364,11 @@ "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": { "version": "1.0.0", "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_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": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/on-exit-leak-free/-/on-exit-leak-free-2.1.2.tgz", diff --git a/package.json b/package.json index a336d8d..072af0c 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "fastify": "^4.26.0", "knex": "^3.1.0", "knex-paginate": "^3.1.1", - "lodash": "^4.17.21", + "nunjucks": "^3.2.4", "sqlite3": "^5.1.7" } } diff --git a/templates/todo.html b/templates/todo.html index cc3a6d8..890bf45 100644 --- a/templates/todo.html +++ b/templates/todo.html @@ -15,12 +15,14 @@ -

<%= your_todos %>

+

Your TODOs

    - <% for(let todo of todo_list) { %> -
  1. <%= todo.task %>
  2. - <% } %> + {% for todo in todo_list %} +
  3. {{ todo.task }}
  4. + {% else %} +
  5. No TODO Items
  6. + {% endfor %}