Comments (7)
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.
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.
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.
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.
I wasn't suggesting per-block imports but per-file imports. It feels to me like macros and inheritance should operate independently.
from askama.
Apparently jinja2 is fine with this? We're supposed to inherit whatever the "parents" imported.
from askama.
Anyway, updated #991 to prevent inheriting variables.
from askama.
Related Issues (20)
- `include` is not very efficient HOT 3
- 0.13.0 release planning HOT 2
- Add tests for generated code HOT 2
- When using askama_axum, the automatic import of IntoResponse will preferentially import from askama_axum instead of axum::response. HOT 4
- Deny all / more reserved keywords as macro names
- Using block fragments as part of larger template HOT 4
- Unnecessary fields required when using blocks for partial rendering HOT 3
- Add `elif` keyword HOT 2
- Template Rendering of a Result<T + Display, E + Display> Cleanly HOT 3
- Feature/docs request: allow setting escape="none" in askama.toml HOT 2
- Include statement has no effect when used from a block fragment HOT 7
- Feedback for me HOT 3
- Add support for remaining pattern syntax (..) in templates
- Unable to publish package containing askama HOT 1
- rendering nested block broken by if-else-endif in outer block HOT 8
- Conditional compiling - features HOT 1
- Children like support for macros / included templates
- Lazy loading or runtime templates HOT 3
- Template parsing issues when using double angle brackets as delimiters
- Dynamically rendering templates during development? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from askama.