-
-
Notifications
You must be signed in to change notification settings - Fork 221
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
Iterate over NewType Vec<T> #207
Comments
Try implementing |
@JulianSza from the Rust #[derive(Template)]
#[template(path = "handlers.html", print="all")]
struct VA(Vec<A>);
impl IntoIterator for VA {
type Item = A;
type IntoIter = std::vec::IntoIter<A>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
} And then you'd use it like so in the template: {% for a in va %}
...
{% endfor %} |
Thanks for quick responses and forgive the stupidity; this is my first time in Rust. I think my problem is more basic than that; I am struggling to find a iterator's 'handle' from the template. Even after implementing The code below will fail with #[macro_use]
extern crate askama;
use askama::Template;
#[derive(Template)]
#[template(path = "handlers.html", print="all")]
struct VA(Vec<A>);
type A = String;
impl VA {
fn new () -> VA {
VA(Vec::new())
}
}
impl IntoIterator for VA {
type Item = A;
type IntoIter = std::vec::IntoIter<A>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl std::ops::Deref for VA {
type Target = Vec<A>;
fn deref(&self) -> &Vec<A> {
&self.0
}
}
impl std::ops::DerefMut for VA {
fn deref_mut(&mut self) -> &mut Vec<A>{
&mut self.0
}
}
fn main() {
let va = VA::new();
dbg!(va.render().unwrap());
}
//handlers.html
// {% for a in va %} <<---- this is the line that makes me cry
// {{ a }}
// {% endfor %} |
In this case your variable |
Yup, tried that too, this one will result in
Compiler will say Which led me into thinking I need to iterate Edit: I should mention that I am looking to get |
Is there a reason it needs to be a NewType and not a struct with a single field? |
Yes, it is because I want it this way:) Using a struct with single field works fine. But it looks ugly in my opinion. If the answer is 'you cannot have it' then I am fine with it. But if NewType is supported by askama then I would like to figure out how to use it |
Totally understand, just curious :) |
Yeah, so this is not quite well supported right now. It would probably be fairly easy to fix it, though, such that |
Thank you thank you thank you, this is exactly how I wanted to use it! |
Can I use a NewType wrapper around
Vec<T>
infor x in y
template file?If I use something like
struct X {values: Vec<Y>}
then I can easily get its data usingfor x in values
. This template will compile tofor (_loop_index, x) in (&self.values).iter_into().enumerate()
.But I want to use NewType instead of a normal struct.
So I need to iterate over
self.0
-> how do I write template for that?for x in self.0
returnsor (_loop_index, x) in (&self.self.0).into_iter().enumerate()
for x in 0
returnsfor (_loop_index, x) in (&0).into_iter().enumerate()
Is there something obvious that I am missing?
This is the struct I am playing with and the template in question:
The text was updated successfully, but these errors were encountered: