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

Rationale behind blocks only at top level #85

Closed
anna-is-cute opened this issue May 17, 2018 · 7 comments
Closed

Rationale behind blocks only at top level #85

anna-is-cute opened this issue May 17, 2018 · 7 comments

Comments

@anna-is-cute
Copy link
Contributor

anna-is-cute commented May 17, 2018

Hello! I recently discovered askama, and I'm very excited about moving to it over Tera. I don't fully understand everything right now, but in trying to migrate, I discovered a confusing error.

I read that blocks can't be in loops, and that makes sense. However, I'm slightly confused as to why my templates, which include no blocks in loops, etc. aren't compiling. I get blocks ('header') are only allowed at the top level.

{% extends "frame.html.j2" %}

{% block head %}
...
{% endblock head %}

{% block body %}
...
{% block header %}
...
{% endblock header %}
...
{% block main %}{% endblock main %}
...
{% endblock body %}

header is inside of body, but I really don't understand why this isn't allowed. In fact, it's extremely useful to be able to provide bits inside of larger blocks that can be overwritten.

In addition, blocks lack the ability to do something similar to {{ super() }} from Tera, which lets you include the block's content instead of overwriting it, which is extremely useful. (Unless you can do so by doing {{ _parent }}. I don't really understand _parent, to be honest. My base template frames have no context, so having to make empty structs for them is confusing.)

What's the rationale behind denying this? It's a gigantic blocker for me, and I really want to use askama.

@djc
Copy link
Collaborator

djc commented May 17, 2018

It's basically been there from when I started on inheritance to make things easier, and no one has asked for it until now. I think with the current design of how inheritance works, these can be accomodated, but I'll have to dig into it a little (unless you want to take a look, I can provide some guidance where to look in the code).

@anna-is-cute
Copy link
Contributor Author

anna-is-cute commented May 17, 2018

Thanks for the quick response!

I'll have to dig into it a little (unless you want to take a look

Unfortunately, it seems like a large feature to look into adding when I'm completely unfamiliar with the codebase, especially if even you would need to dig into it, so I'd rather leave it to the professionals. 😃

@djc djc closed this as completed in 60950c9 May 20, 2018
@djc
Copy link
Collaborator

djc commented Jun 22, 2018

Master now implements deep inheritance and the super() macro. I'll release a 0.7.0 soon.

@anna-is-cute
Copy link
Contributor Author

Ooooh for the first time ever my askama branch compiles! You're the best!

@djc
Copy link
Collaborator

djc commented Jun 22, 2018

Note that you also no longer need the _parent fields, you can now just use a flattened struct including all the required fields.

@jmafc
Copy link

jmafc commented May 29, 2020

I have a base template in a Flask app that I'm porting that has something like the following in the <head>:

  {% if page.no_index %}
    <meta name="robots" content="noindex,nofollow">
  {%- else %}
    {%- block meta_x %}{% endblock %}
  {%- endif %}

Various other templates then implement the meta_x block in at least three different ways. Askama doesn't like it and complains with a message just like the one in the original comment above. The closing/comments in May/June 2018 would lead one to believe that this should not happen, but it appears that although straight nesting of a block inside a block is now allowed, nesting inside an if block is not? Is that something that has a workaround (other than putting the if inside a meta_x block, which in my case would require surgery on almost all templates)?

@djc
Copy link
Collaborator

djc commented May 29, 2020

Yeah, I think that limitation might still be in place. I guess I was too lazy to write the more generic code that can walk the entire AST, and there's some argument that doing so will incur more compile-time costs. Probably should just fix it anyway. Would you mind filing a new issue about it? (Bonus points if you also want to try your hand at a PR. 😄 The relevant code is here: https://github.com/djc/askama/blob/master/askama_shared/src/heritage.rs#L45.)

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