Found out that the templates generated by lodash (and all template engines) don't connect back to the original template file so you lose line numbers. Not sure how to solve it.

main
Zed A. Shaw 9 months ago
parent 44f7603ddc
commit 7284e510d8
  1. 33
      bin/app.js
  2. 25
      templates/todo.html

@ -3,29 +3,36 @@ 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 _ from "lodash"; import template from "lodash/template.js";
const fastify = Fastify({ const fastify = Fastify({
logger: true logger: true
}); });
fastify.get("/todo", async (request, reply) => { fastify.get("/todo", async (req, rep) => {
const list = await ToDo.all({}); try {
const todo_list = await ToDo.all({});
return list; // 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"});
fastify.get("/todo.html", async (req, rep) => { console.log("FUNCTION-----\n", tmpl.toString());
const list = await ToDo.all({});
const tmpl_src = fs.readFileSync("templates/todo.html"); const result = tmpl({todo_list});
const tmpl = _.template(tmpl_src, );
const result = tmpl({message: "Hello!"}); rep.code(200)
.type("text/html")
.send(result);
} catch(error) {
console.error(error);
console.error(error.stack);
console.error(error.source);
}
});
fastify.get("/todo.html", async (req, rep) => {
rep.code(200)
.type("text/html")
.send(result);
}); });
fastify.register(FastifyStatic, { fastify.register(FastifyStatic, {

@ -3,27 +3,24 @@
<html> <html>
<head> <head>
<script src="/alpine.js"></script> <script src="/alpine.js"></script>
<script src="/fsm.js"></script>
<title>Bandolier2</title> <title>Bandolier2</title>
<style> <style>
@import "/open-props.min.css"; @import "/open-props.min.css";
@import "/normalize.min.css";
</style>
blockquote { <head>
--_accent-1: var(--lime-5); </head>
--_accent-2: var(--lime-4);
--_bg: var(--surface-2);
--_ink: var(--text-1);
color: var(--_ink);
border-color: var(--_accent-2);
background-color: var(--_bg);
justify-self: flex-start;
}
</style>
<head>
<body> <body>
<h1><%= message %></h1> <h1><%= your_todos %></h1>
<p>This is a template!</p> <ol>
<% for(let todo of todo_list) { %>
<li><%= todo.task %></li>
<% } %>
</ol>
</body> </body>
</html> </html>

Loading…
Cancel
Save