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

[feature] Add "embed" block #488

Open
YannikSc opened this issue May 25, 2021 · 11 comments
Open

[feature] Add "embed" block #488

YannikSc opened this issue May 25, 2021 · 11 comments

Comments

@YannikSc
Copy link

Having an "embed" block like twig has it, which is basically an "extend" but without an extra file, would be an awesome addition to the library I think.

Is it in the current implementiation possible to get something like this working (or is there maybe even something like this already)?

This would be very useful if a macro is just to small or you want to get more markup replaced in a general structure (like a modal).

@vallentin
Copy link
Collaborator

which is basically an "extend" but without an extra file

What do you mean by "without an extra file"? Unless I'm misinterpreting something from the docs you linked, then you still need all your (partial) templates in separate files. So with embed you're able to include a separate template and without the embed scope you're able to override the blocks.

I like it and can personally see many uses. Just want to make sure I didn't miss anything.

@djc
Copy link
Owner

djc commented May 25, 2021

I'm open to it in principle as long as it doesn't add too much complexity in the code generator; if it can reuse most of the existing infrastructure for extends/block, I'm fine with adding another interface to it.

@YannikSc
Copy link
Author

"without an extra file" means, that you don't need an extra file to extend the base template. Normally when you want to extend something, you use 3 files. For the parent and child template, and the template, where you include it. With this embed you only have 2. The parent and the including template which is doing what the child template is doing but inside of the embed.

I'm just describing it badly. The linked documentation however exaclty describes, what I'm trying to explain.

@vallentin
Copy link
Collaborator

vallentin commented May 26, 2021

I'm still not sure I understand the "without an extra file", maybe I'm tired, I don't know. :)

If we go with your original example, you have a page template and a modal template. If the modal template contains a macro, then you need to import and call. If it instead is a "sub-page", then we instead embed it. But overall, we still have 2 files, right?

Even if we're talking about complex template inheritance. Say you have a base template, a page template, then an error template. They each extend each other, in that order. Then you finally have a not-found and internal-server-error templates, that both extend error. Maybe instead of extending error you could extend page, and then embed error. But once again, that doesn't decrease the amount of files?

I'm clearly missing something. :)

Regardless, I still think embed is nice and useful, in relation to replacing e.g. macros with embed, as I have quite a few of those myself. :)

@YannikSc
Copy link
Author

Okay then; here the use case I had:

Building a modal.html with the structure for a model: header, body and footer, controls etc.
Now I wanted to use this modal.html on my page. However for the modal to be useful I would have to create a concrete-modal.html which extends the modal template and adds the content for the body (still possible with blocks etc.).

But much more pleasing (for page-unique modals at least) to just embed the modal.html and add the body content directly on the page.

I built another modal with a macro and just pass a different id so that I can idetify it through JS. But in this case it was possible because I used the same body on all pages.

Hope this can clarify it by now :D

@vallentin
Copy link
Collaborator

Yeah, I can follow that use case, I had something similar in mind. But I still don't entirely understand how it would decrease the files needed? :)

Ultimately isn't concrete-modal.html your final page? If so, isn't that needed regardless whether modal.html is a parent that's extended or whether it's just a separate file that is embedded? :)

@djc
Copy link
Owner

djc commented Oct 25, 2023

Before I try to review your PR, I'd like to see a fully worked example of the template files you think you need without this feature and the templates you'd need if this feature was available, to make sure I understand what you're trying to achieve.

@YannikSc
Copy link
Author

Sure, I will put you together an example, I hope it's fine when I omit any JavaScript and CSS parts from it

@djc
Copy link
Owner

djc commented Oct 25, 2023

Sure! It doesn't need to be detailed, minimalism is probably best here though a little meaning can help understand the context.

@YannikSc
Copy link
Author

It took a while and I only have a broken example, but it at least shows off what I am (or was) trying to achieve.

I'm not sure what exactly is wrong with my part of the code, I cannot find the error message, that is printed but I had it before and fixed it somehow.

But I also saw, that it seems to be impossible to include a template, which extends another one ("extend blocks only allowed at the top level"). Not sure if this is expected, but I would expect both examples to work. I will try around to fix my code, but it might clear up what exactly I want to do.

@YannikSc
Copy link
Author

So after some checking I fixed one issue on my side and according to the code and the AstLevel::Nested for the include template, the include and extends behavior seems to be intended too (although I would question if this should really be like that).

With the extends removed from the included templates however I get a "no entry found for key". If I read the code correctly this is because includes, that are inside a block will not get checked by the find_used_templates method, thus the template is not available inside the context. This again brings up the question for me if this is a bug or just wrong use of the library?

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

3 participants