Code Monkey home page Code Monkey logo

Comments (7)

djc avatar djc commented on August 23, 2024

So you're expecting that macro scopes imported in the base are available in the child template?

Seems kinda reasonable, let's see how much complexity in the generator it requires?

On the other hand, it doesn't seem too crazy to make imports file-scoped...

from askama.

djc avatar djc commented on August 23, 2024

So this is supported by Tera, right? What about Jinja? I'm still on the fence about whether this is a good idea -- file-scoped imports actually seem pretty reasonable to me, similar to how things work in Rust.

And, this is actually easy to work around, right?

from askama.

GuillaumeGomez avatar GuillaumeGomez commented on August 23, 2024

No it's not unfortunately, especially when extends comes in. It means you have to re-import everything you need inside your block, which is pretty bad, especially when the information is already around.

As for original jinja support, I tested this code and it works:

foo.py:

from jinja2 import Environment, PackageLoader, select_autoescape
env = Environment(
    loader=PackageLoader("foo"),
    autoescape=select_autoescape()
)

template = env.get_template("mytemplate.html")
template.render(the="variables", go="here")

mytemplate.html:

{% extends "extend_and_import.html" %}
{%- import "macro.html" as m2 -%}

{%- macro another(param) -%}
--> {{ param }}
{%- endmacro -%}

{% block header -%}
{{ m1.twice(1) }}
{{ m2.twice(2) }}
{{ another(3) }}
{%- endblock -%}

extend_and_import.html:

{% import "macro.html" as m1 %}

{% block header %}{% endblock %}

macro.html:

{%- macro twice(param) -%}
{{param}} {{param}}
{% endmacro %}

(you'll note that scopes can be accessed with . and not ::, funny discovery)

from askama.

GuillaumeGomez avatar GuillaumeGomez commented on August 23, 2024

The whole explanation is here. And I also need to not allow access to variables which are not defined in the block in my PR (currently it does allow it, which is invalid based on the description).

from askama.

djc avatar djc commented on August 23, 2024

I wasn't suggesting per-block imports but per-file imports. It feels to me like macros and inheritance should operate independently.

from askama.

GuillaumeGomez avatar GuillaumeGomez commented on August 23, 2024

Apparently jinja2 is fine with this? We're supposed to inherit whatever the "parents" imported.

from askama.

GuillaumeGomez avatar GuillaumeGomez commented on August 23, 2024

Anyway, updated #991 to prevent inheriting variables.

from askama.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.