From 7284e510d8e831dd9c52a54da22f85a60ba19420 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sun, 18 Feb 2024 03:54:55 -0500 Subject: [PATCH] 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. --- bin/app.js | 33 ++++++++++++++++++++------------- templates/todo.html | 25 +++++++++++-------------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/bin/app.js b/bin/app.js index 79e53be..7e2d78c 100644 --- a/bin/app.js +++ b/bin/app.js @@ -3,29 +3,36 @@ import FastifyStatic from "@fastify/static"; import path from "path"; import { ToDo } from "../lib/models.js"; import fs from "fs"; -import _ from "lodash"; +import template from "lodash/template.js"; const fastify = Fastify({ logger: true }); -fastify.get("/todo", async (request, reply) => { - const list = await ToDo.all({}); +fastify.get("/todo", async (req, rep) => { + 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) => { - const list = await ToDo.all({}); + console.log("FUNCTION-----\n", tmpl.toString()); - const tmpl_src = fs.readFileSync("templates/todo.html"); - const tmpl = _.template(tmpl_src, ); + const result = tmpl({todo_list}); - 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, { diff --git a/templates/todo.html b/templates/todo.html index 7c8a6bb..cc3a6d8 100644 --- a/templates/todo.html +++ b/templates/todo.html @@ -3,27 +3,24 @@ + Bandolier2 -blockquote { - --_accent-1: var(--lime-5); - --_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; -} - - -

<%= message %>

+

<%= your_todos %>

-

This is a template!

+
    + <% for(let todo of todo_list) { %> +
  1. <%= todo.task %>
  2. + <% } %> +