Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load templates from external source #256

Open
KirioXX opened this issue Sep 9, 2023 · 3 comments
Open

Load templates from external source #256

KirioXX opened this issue Sep 9, 2023 · 3 comments

Comments

@KirioXX
Copy link

KirioXX commented Sep 9, 2023

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.

@nebrelbug
Copy link
Collaborator

@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.

@KirioXX
Copy link
Author

KirioXX commented Sep 13, 2023

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/[email protected]/http/server.ts>";
import { Eta } from "<https://deno.land/x/[email protected]/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/[email protected]/src/file-handling.ts:59:25>)
    at Eta.resolvePath (<https://deno.land/x/[email protected]/src/file-handling.ts:48:7>)
    at Eta.render (<https://deno.land/x/[email protected]/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/[email protected]/http/server.ts:221:37>)
    at Server.#serveHttp (<https://deno.land/[email protected]/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!

@nebrelbug
Copy link
Collaborator

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants