Example of passing a structs to HTML Template #446
-
I want to use the new template views feature of loco and i'am stuck on passing a struct to render a list. I'am using the "Loco Guide" Article Models as an example Snippet from template: <tbody>
{% for article in articles %}
<tr>
<td>{{ article.id }}</td>
<td>{{ article.title }}</td>
<td>{{ article.content }}</td>
</tr>
{% endfor %}
</tbody> dashboard.rs use crate::models::_entities::articles::Model;
pub fn home(v: impl ViewRenderer, articles: Vec<Model>) -> Result<impl IntoResponse> {
format::render().view(&v, "home/index.html", json!(articles))
} I'am getting this error:
Can you provide an example how to render a html list? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I have found a solution by myself :-) , after I saw that this package is being used: #[derive(Serialize)]
struct Response {
articles: Vec<Model>,
}
pub fn home(v: impl ViewRenderer, articles: Vec<Model>) -> Result<impl IntoResponse> {
format::render().view(&v, "home/index.html", Response { articles })
} The "trick" is to wrap your data with an additional struct. |
Beta Was this translation helpful? Give feedback.
I have found a solution by myself :-) , after I saw that this package is being used:
https://keats.github.io/tera/docs/
The "trick" is to wrap your data with an additional struct.