diff --git a/src/main.rs b/src/main.rs index de5f0de..405df10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,52 +5,54 @@ use actix_web::{get, middleware, App, HttpRequest, HttpServer, Responder}; use actix_web_lab::respond::Html; use askama::Template; -#[derive(Template)] -#[template(path = "layout.html")] -struct Index; - -#[derive(Clone, Debug)] -struct Row { - foo: String, - bar: String, - baz: String, +struct User { + name: String, + email: String, } #[derive(Template)] -#[template(path = "fragment/table.html")] -struct Table { - table: Vec, +#[template(path = "fragment/user/table.html")] +struct Fragment { + users: Option>, +} + +#[derive(Default, Template)] +#[template(path = "index.html")] +struct Document { + // TODO: This is duplicated. Reuse the Fragment struct. But how? + users: Option>, } #[get("/")] async fn index() -> actix_web::Result { - Ok(Html(Index.render().expect("Valid template"))) + let page = Document { users: None }; + Ok(Html(page.render().expect("Valid template"))) } -#[get("/table")] -async fn table(req: HttpRequest) -> actix_web::Result { +#[get("/user")] +async fn users(req: HttpRequest) -> actix_web::Result { + let users = Some(vec![ + User { + name: "Jon Doe".into(), + email: "doe@foo.baz".into(), + }, + User { + name: "Jane Doe".into(), + email: "j.doe@foo.baz".into(), + }, + User { + name: "Joe Doe".into(), + email: "joe.doe@foo.baz".into(), + }, + ]); match req.headers().get("hx-request") { Some(_) => { // Render the hypermedia fragment. - Ok(Html( - Table { - table: vec![ - Row { - foo: "foo".into(), - bar: "bar".into(), - baz: "baz".into() - }; - 10 - ], - } - .render() - .expect("Valid template"), - )) + Ok(Html(Fragment { users }.render().expect("Valid template"))) } None => { // Render the whole document - // FIXME: Render the fragment in place... - Ok(Html(Index.render().expect("Valid template"))) + Ok(Html(Document { users }.render().expect("Valid template"))) } } } @@ -66,7 +68,7 @@ async fn main() -> io::Result<()> { .wrap(middleware::Logger::default()) .service(fs::Files::new("/static", "static").show_files_listing()) .service(index) - .service(table) + .service(users) }) .bind(("127.0.0.1", 8080))? .run() diff --git a/templates/fragment/table.html b/templates/fragment/table.html deleted file mode 100644 index 6e3b478..0000000 --- a/templates/fragment/table.html +++ /dev/null @@ -1,8 +0,0 @@ - - foobarbaz - - - {% for row in table %} - {{ row.foo }}{{ row.bar }}{{ row.baz }} - {% endfor %} - diff --git a/templates/fragment/user/table.html b/templates/fragment/user/table.html new file mode 100644 index 0000000..0c3a93c --- /dev/null +++ b/templates/fragment/user/table.html @@ -0,0 +1,7 @@ +nameemail +{% for user in users.as_ref().unwrap() %} + + {{ user.name }} + {{ user.email }} + +{% endfor %} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..b157be0 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,19 @@ +{% extends "layout.html" %} + +{% block content %} +
+

Hello htmx!

+

A new old way to build web applications

+
+ + {% if users.is_some() %} + {% include "fragment/user/table.html" %} + {% else %} + + {% endif %} +
+ +
+{% endblock %} diff --git a/templates/layout.html b/templates/layout.html index d0d78e8..47a0a21 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -9,20 +9,13 @@ - Hello htmx! + {% block title %}Hello htmx!{% endblock %} + + {% block head %}{% endblock %} -
-

Hello htmx!

-

A new old way to build web applications

-
- - - -
- -
+
+ {% block content %}{% endblock %} +