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

template-fragments #1441

Open
johntom opened this issue Jun 11, 2023 · 5 comments
Open

template-fragments #1441

johntom opened this issue Jun 11, 2023 · 5 comments

Comments

@johntom
Copy link

johntom commented Jun 11, 2023

Hi,
I've been using htmx and they have a section in docs called https://htmx.org/essays/template-fragments/. I then found out that fragments are supported in Apostroph 3 (https://v3.docs.apostrophecms.org/guide/fragments.html#using-fragments) which uses numjucks as its template engine and I'm curious if ...

  1. Are they using fragments in the same way as jinja2?
  2. Do you plan to support template-fragments in a future release?
    Very happy user using fastify/nunjucks and htmx.
    TIA
    John
@NextThread
Copy link

hey, can I work on this issue

@webketje
Copy link

webketje commented Nov 2, 2023

I think this should not be in core. It is a requirement specific to the usage of a third-party software implementing server-driven partial hydration of front-end views, a concern that is not in scope of the Nunjucks templating library. For all other flows and purposes, asynchronous data should be loaded prior to a synchronous Nunjucks render call, or should use an asynchronous custom loader together with an async Nunjucks render call

@benkenawell
Copy link

Hi,

I realize this issue is ~1 year old, but I would like to make an argument for it being in core.

If you read the Template Fragments Essay, this feature is about being able to render part of a template out of the greater whole. It is a method of breaking down templates within a single file, a method of template decomposition. It feels complementary to, and maybe inverse of, template inheritance which builds up a single template from decomposed files.

There is no requirement in HTMX, or any server-driven partial hydration methodologies, for this type of template fragmenting. The essay only points out that being able to stay in context, within the file, when writing template files is more convenient. It makes it easier to follow what a page might look like when different templating paths are taken. Whether I return an entirely new page or just a partial page, it still helps to be able to follow what the templated code will look like.

Today, most websites take advantage of partially updating the webpage, they don't often do a full-page refresh. Currently, most partial page updates are still done in JS via React or other client side library, and wouldn't utilize a templating library like Nunjucks at all. Increasingly, libraries like HTMX, Hotwire, and Data Star are becoming more popular. They all allow for easy partial page renders when the server return HTML, making templating libraries very attractive in conjunction with Express/Fastify. Currently, every time I want to make a partial page update, I need to break that part of the page out into its own file instead of just being able to render part of the template. Very quickly when testing a page, I ended up with a layout template, page template, and 3 partials for barebones partial page updates; there are many more things I would still like to break out of my page template! Template fragmenting is a very attractive solution to this problem.

Personally, I like how JStachio handles rendering these fragments though a URI Fragment Notation. It gives all of the flexibility without any additional API surface area. It seems something should be possible using a Custom Extension/Tag or extending the Environment, but nothing compares to the API of that URI Fragment Notation.

If someone would be willing to offer some guidance, I would be happy to try my hand at one of the above. Even if it ends up in a separate package, not core, it would be a great benefit for me.

@dougmoscrop
Copy link

dougmoscrop commented Jun 29, 2024

Ampt took a stab at this, but I think if this were in core it would be even more powerful: https://github.com/getampt/fraglates

@benkenawell
Copy link

Wow, thanks for the link, that looks like what I was looking for, plus some extra. I will have to take it for a test drive soon!

I may still attempt a thinner wrapper, instead of a full replacement. I want to see how far I can go with an extension or a tiny bit of monkey patching.

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

5 participants