I'm Rust beginner. I want to compile this file, but i get error.
Because I wrote #[derive(Template)], I think that it should be inheritable.
Am I missing something?
error: unable to parse template:
"{% for entry in entries %}\n <div>\n <div>id:{{ entry.id}}, text: {{entry.text}}</div>\n <form action=\"/delete\" method=\"POST\">\n <input type=\"hidden\" name=\"id\" value=\"{{entry.id}}\">\n <button>delete</button>\n </form>\n </div>\n {% end for %}\n </div>\n <form action=\"/add\" method=\"POST\">\n <div>\n <input nme=\"text\">\n </div>\n <div>\n <button>add</button>\n </div>\n </form>\n </body>\n</html>"
--> src/main.rs:64:10
|
64 | #[derive(Template)]
| ^^^^^^^^
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0599]: no method named `render` found for struct `IndexTemplate` in the current scope
--> src/main.rs:89:30
|
66 | struct IndexTemplate {
| -------------------- method `render` not found for this
...
89 | let response_body = html.render()?;
| ^^^^^^ method not found in `IndexTemplate`
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `render`, perhaps you need to implement it:
candidate #1: `Template`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0599`.
error: could not compile `todo`
main.rs
use actix_web::{get, App, HttpResponse, HttpServer, ResponseError};
use askama::Template;
use thiserror::Error;
struct TodoEntry {
id: u32,
text: String,
}
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {
entries: Vec<TodoEntry>,
}
#[derive(Error, Debug)]
enum MyError {
#[error("Failed to render HTML")]
AskamaError(#[from] askama::Error),
}
impl ResponseError for MyError {}
#[get("/")]
async fn index() -> Result<HttpResponse, MyError> {
let mut entries = Vec::new();
entries.push(TodoEntry {
id: 1,
text: "First entry".to_string(),
});
entries.push(TodoEntry {
id: 2,
text: "Second entry".to_string(),
});
let html = IndexTemplate { entries };
let response_body = html.render()?;
Ok(HttpResponse::Ok()
.content_type("text/html")
.body(response_body))
}
#[actix_rt::main]
async fn main() -> Result<(), actix_web::Error> {
HttpServer::new(move || App::new().service(index))
.bind("0.0.0.0:8080")?
.run()
.await?;
Ok(())
}
Cargo.toml
[dependencies]
actix-rt = "2.2.0"
actix-web = "3.3.2"
askama = "0.10.5"
thiserror = "1.0.24"
`
templates/index.html
`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Todo App</title>
</head>
<body>
<div>
{% for entry in entries %}
<div>
<div>id:{{ entry.id}}, text: {{entry.text}}</div>
<form action="/delete" method="POST">
<input type="hidden" name="id" value="{{entry.id}}">
<button>delete</button>
</form>
</div>
{% end for %}
</div>
<form action="/add" method="POST">
<div>
<input nme="text">
</div>
<div>
<button>add</button>
</div>
</form>
</body>
</html>