eta-dev / eta

Embedded JS template engine for Node, Deno, and the browser. Lighweight, fast, and pluggable. Written in TypeScript

Home Page:https://eta.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Load templates from external source

KirioXX opened this issue · comments

Is your feature request related to a problem? Please describe.
I trie to run eta in a supabase edge function (deno deploy) those function seam not to support the import for template files.
As a alternative to that I was thinking it would be great if we could import the templates from a Github repository or a S3 bucket.

Describe the solution you'd like
As a alternative to instead of a folder path for views specifying a url to a external source that contains the templates.

Describe alternatives you've considered
I think about using a inline template but that is failing at the moment because the config seams to require a views path.

Additional context
Add any other context or screenshots about the feature request here.

@KirioXX could you provide a simple example? Import should work even with edge functions, I've tested it with Deno Deploy.

If you need you can just pass in a dummy string to config.views.

I like the idea of allowing URL import, but it should probably work through an Eta plugin rather than be added to the source code.

Thanks for coming back to me @nebrelbug.

I just used the quick start example to build the following function (the template folder is in the same folder as the index.ts):

import { serve } from "<https://deno.land/std@0.168.0/http/server.ts>";
import { Eta } from "<https://deno.land/x/eta@v3.0.3/src/index.ts>";

const eta = new Eta({ views: "./templates" });

console.log("Hello from Functions!");

serve(() => {
  const res = eta.render("./simple", { name: "world" });
  console.log(res);

  return new Response(
    res,
    { headers: { "Content-Type": "application/html" } },
  );
});

When I call this function I get this error:

[Error] TypeError: Resolved a relative path without a CWD.
    at resolve (ext:deno_node/path/_posix.ts:25:15)
    at Module.relative (ext:deno_node/path/_posix.ts:94:10)
    at dirIsChild (<https://deno.land/x/eta@v3.0.3/src/file-handling.ts:59:25>)
    at Eta.resolvePath (<https://deno.land/x/eta@v3.0.3/src/file-handling.ts:48:7>)
    at Eta.render (<https://deno.land/x/eta@v3.0.3/src/render.ts:32:31>)
    at Server.<anonymous> (file:///home/deno/functions/quality-pack-pdf/index.ts:11:19)
    at Server.#respond (<https://deno.land/std@0.168.0/http/server.ts:221:37>)
    at Server.#serveHttp (<https://deno.land/std@0.168.0/http/server.ts:258:20>)
    at eventLoopTick (ext:core/01_core.js:183:11)

Did you import the templates differently?

To provide more context on why it would be beneficial to import templates from an external source, consider the following. I am looking to create a platform similar to [Apitemplate](https://apitemplate.io/). The idea is to give non-technical personnel within the company a playground where they can modify PDF templates using only HTML and CSS. If we can load the templates from a separate repository, the playground could exist independently of our main backend. This would allow changes to be made without affecting the history of our main project.

Thank you!

@KirioXX sounds like a cool project! I think I've tracked the error down. Will you try seeing if you can call Deno.cwd() from within an edge function? Is Supabase using the most recent Deno version?

There's a chance that filesystem operations, which now work with Deno Deploy, still don't work with Supabase edge functions. If that's the case, the best option may be to create an Eta plugin or pass through a function that can fetch remote templates.

Or, you can define templates as strings and then load them using Eta.templates.define("name", ...), then reference using include("@name") (see https://eta.js.org/docs/intro/template-syntax#name-resolution-of-partials-and-layouts).

See also #172.