diff --git a/src/main.rs b/src/main.rs index 405df10..637df12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,13 +19,12 @@ struct Fragment { #[derive(Default, Template)] #[template(path = "index.html")] struct Document { - // TODO: This is duplicated. Reuse the Fragment struct. But how? - users: Option>, + fragment: Option, } #[get("/")] async fn index() -> actix_web::Result { - let page = Document { users: None }; + let page = Document { fragment: None }; Ok(Html(page.render().expect("Valid template"))) } @@ -52,7 +51,8 @@ async fn users(req: HttpRequest) -> actix_web::Result { } None => { // Render the whole document - Ok(Html(Document { users }.render().expect("Valid template"))) + let fragment = Some(Fragment { users }); + Ok(Html(Document { fragment }.render().expect("Valid template"))) } } } diff --git a/templates/index.html b/templates/index.html index b157be0..e7b34b7 100644 --- a/templates/index.html +++ b/templates/index.html @@ -6,14 +6,16 @@

A new old way to build web applications

- {% if users.is_some() %} - {% include "fragment/user/table.html" %} - {% else %} - - {% endif %} + + {% match fragment %} + {% when Some with (fragment) %} + {{ fragment|safe }} + {% when None %} + + {% endmatch %}
- -
+ +
{% endblock %}