Comments (6)
That certainly sounds surprising. Do you have a minimal reproducing example?
from askama.
This is the simplest reproducing example I could get:
macro_rules! test_macro{
($entity:expr) => {
{
println!("{:?}", &$entity);
}
}
}
#[derive(Template)]
#[template(source = "{{ test_macro!(entity) }}", ext = "txt")]
struct TestTemplate<'a> {
entity: &'a str,
}
which results in:
error[E0425]: cannot find value `entity` in this scope
--> src/.../main.rs:29:10
|
29 | #[derive(Template)]
It looks like it works if I call the macro using self.entity
, but this is not what I want, because I'd like to reuse the template in a situation where it's nested in a loop.
from askama.
Oh I see the issue. Since it's a macro, we can't know if entity
is from self
or just a token (for a name you're generating for example?). I think this code would work in all cases:
let entity = entity;
{{ test_macro!(entity) }}
from askama.
I assume you meant:
{% let entity = entity; %}
{{ test_macro!(entity) }}
which seems to work indeed.
Would it be possible for Askama to automatically bind all struct fields from the template struct to local variables with the same name so that macros will work automatically?
from askama.
I assume you meant:
...
Yes I did. ^^'
Would it be possible for Askama to automatically bind all struct fields from the template struct to local variables with the same name so that macros will work automatically?
It'd be problematic for different reasons:
- If you have a big struct, it'll generate a lot of variables that might not be used. Lot of dead code in short, meaning bigger code to compile, meaning longer compile time.
- It could lead to surprising/unwanted usage of variables. For example you use a variable named
x
in a template you include. Variable you're supposed to define yourself which you do normally because of the error. But if you type has anx
field, then it'll just be a surprising (and likely unwanted) result.
What I suggest is instead to add your use case as an example for macros in the askama book. What do you think?
from askama.
Thanks for the feedback! I understand. Feel free to add it to the book!
from askama.
Related Issues (20)
- Do we need a built-in yaml filter? HOT 4
- Template compilation infinite loop / failed cycle detection HOT 4
- Add display_some filter and friends HOT 14
- Float literals are not completely supported HOT 1
- Add support for single quote strings? HOT 7
- Allow output of not prettified JSON data
- Compilation error when using nested template blocks with in a single template file HOT 3
- HELP: about askama_axum::Template HOT 2
- Nested comments causes parser to timeout HOT 2
- Publish all crate versions books HOT 7
- if let chain has a weird behaviour HOT 2
- `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
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.