askama icon indicating copy to clipboard operation
askama copied to clipboard

Allow inlined extending within included templates.

Open genusistimelord opened this issue 2 years ago • 13 comments

So here is the issue, in the current Jinja i can use a card.html which include something like this

<div class="uk-card uk-card-default">
    <div class="uk-card-header">
        <span class="uk-card-title">
            {% block card_title %}{% endblock card_title %}
        </span>
        {% block card_badge %}{% endblock card_badge %}
    </div>
    <div class="uk-card-body">
        {% block card_body %}{% endblock card_body %}
    </div>
    {% block card_footer %}{% endblock card_footer %}
</div>

so then when i go to setup certain things lets say I want to make a Article Card that uses the card.html to extend itself

{% extends "components/card.html" %}

{% block card %}
    {% block card_title %}
            Registration Agreement
    {% endblock card_title %}
    {% block card_body %}
                    <p>This is a random Article</p>
                    <p><b>Last update: August 9, 2018</b></p>
                    <p><b>By continuing you agree to the above privacy policy.</b></p>
    {% endblock card_body %}
{% endblock card %}

This would in jinja load the single extension, Place the changes inside it, then send the changed version down the line to be used in the parent. However Askama does not support this. Would it be Possible to add this or to add it as an inline_extend type? As this is useful for reducing HTML that may get modified to change theme looks ETC without having to go thru 1000 files to do so.

genusistimelord avatar Dec 07 '21 19:12 genusistimelord

Sorry, it's not really clear to me what you mean by this. Can you do a more complete example of what you're trying to achieve in terms of output and how reuse should facilitate that?

djc avatar Dec 07 '21 19:12 djc

ok so here is a example but in python that is working using jinja i will link each file and what they are.

so the base file used for each main page is this: https://github.com/AscendingCreations/acforums/blob/master/auth/templates/base.htm

it is the file that is extended upon as a parent.

the Parent that would use the base would be https://github.com/AscendingCreations/acforums/blob/master/auth/templates/forum/forum.htm

this will load the base and do all the normal extend stuff you support currently BUT the {% include "components/category-card.html" %} portion internals also extend something else which is what you do not support.

so here is that html file https://github.com/AscendingCreations/acforums/blob/master/auth/templates/components/category-card.html

this extends against a internal card file https://github.com/AscendingCreations/acforums/blob/master/auth/templates/components/card.html

Also in the forum file you will also notice that it also {% include "components/who-is-online-card.html" %} which also uses the same extend as the category-card. https://github.com/AscendingCreations/acforums/blob/master/auth/templates/components/who-is-online-card.html

now lets say I wanted to change the way these cards were designed. I could do so by simply changing https://github.com/AscendingCreations/acforums/blob/master/auth/templates/components/card.html

which would update all my other cards that are extending it Otherwise If i do not have the capability I would then need to go to each and every Card that uses the same instance and update them individually.

Does this make more sense? For a working Example of this output when all those parts are rendered you would get something like https://ascendingcreations.com/

which is using the current jinja layouts i posted links to here.

So the fix currently for this for me would be to go thru all of my code and replace all the Blocks with the HTML that card.html contains.

genusistimelord avatar Dec 07 '21 20:12 genusistimelord

I think the major issue is you only allow parents to Extend so in askamas Case it would only allow <- means to extend -> means to include

base<-forum->category-card

for this to work id need to be able to base<-forum->card<-category-card

genusistimelord avatar Dec 07 '21 20:12 genusistimelord

Ahhh, so you want includes to support inheritance? I don't see any fundamental reason why that should not work, might just be an oversight in the parser or code generator. Do you get an error message when you try this?

djc avatar Dec 08 '21 09:12 djc

yeah inheritance would be nice. Currently when i try this it tells me extend only works for the parent. Though we could probably make it more apparent that it is an inheritance rather than an extend. Maybe by making it {% inherit "components/category-card.html" %} rather than {% extend "components/category-card.html" %}. If this makes it easier to introduce and fix. Otherwise if it is a easy fix then I would not worry about the rename. =3.

genusistimelord avatar Dec 08 '21 13:12 genusistimelord

Not sure it is an easy fix. I won't be renaming the keyword, since extends is in wide use in things such as Jinja/Twig as well.

So did you get an error message?

djc avatar Dec 08 '21 13:12 djc

Yes the error is

error: extend blocks only allowed at the top level --> src\main.rs:141:10 | 141 | #[derive(Template, Deserialize, Serialize)]

genusistimelord avatar Dec 08 '21 15:12 genusistimelord

Okay, I don't remember why that is exactly but it does make this somewhat hairy probably. I'm not sure I'll have time to work on this in the near future, but you're welcome to have a look at the code. The relevant code is in askama_shared/src/generator.rs in handle_include().

djc avatar Dec 09 '21 08:12 djc

Yeah so i was somewhat looking into this and it looks as if in order to properly do it back-end each include would need to be loaded as its own thing first generate the end code and the publish it back to the original code. Not entirely 100% sure but I'm having difficulties following it. So if we did it this way it would allow an include to take in a extend without issue and treat it as the parent until it is returned.

genusistimelord avatar Dec 13 '21 14:12 genusistimelord

I don't have objections to that on basis of this high-level description, but I still have a hard time imagining the impact on the code generator so we'd have to see what the sticking points are.

djc avatar Dec 15 '21 09:12 djc

idk from looking at the code I'm still trying to fully understand how you currently do things but it seems for included stuff it doesn't load anything extend wise currently. I am just unsure about it ATM as I'm not the best at parsers XD. Like in my mind I would think that we would Grab the extend and use it as the parent object and then Grab the object extending to it and place its blocked code into the base. Doing this per each part that is extended.

Then after we place all the parts together in each extend we would go and do all the final processing. Handling if statements, Macros etc. This to me seems like a multi parse process similar to compilers. I am still unsure though.

The main point of doing this would be to allow users to simplify their HTML code and reuse portions of it thru extends. This makes it easier to maintain or change an entire websites layout. It is not 100% needed as people could still just write it all out manually. I look at this as more of a if you had to do a theme update or have multiple site themes for a Forum software or something else that this would make doing so much easier to deal with.

But honestly its not 100% needed so it is up to you if this is a jinja feature you want to add or not.

genusistimelord avatar Dec 15 '21 12:12 genusistimelord

Yeah, it's niche enough that I don't have much time to spend on it. I think there are other mechanisms in Askama that help with code re-use and can probably also do a decent job covering this use case.

djc avatar Dec 15 '21 13:12 djc

OK well we can leave this here as a placeholder. Though I was looking at @malyn block partials stuff which would work in a similar way.

genusistimelord avatar Dec 15 '21 13:12 genusistimelord