gumbo_lib/
view.rs

1use actix_web::HttpResponse;
2use yew::html::BaseComponent;
3use yew::ServerRenderer;
4
5/// Render a Yew view to send out in an Actix Response
6pub async fn render<V, VM, E>(args: VM) -> Result<HttpResponse, E>
7where
8    V: BaseComponent,
9    V: BaseComponent<Properties = VM>,
10    VM: Send + 'static,
11{
12    let renderer = ServerRenderer::<V>::with_props(|| args);
13    let html = renderer.render().await;
14    // add the doctype markup. Yew doesn't like to render this.
15    let html = format!("<!DOCTYPE html>\n{html}");
16    Ok(HttpResponse::Ok()
17        .content_type("text/html; charset=utf-8")
18        .body(html))
19}
20
21/// Render a Yew view to send out in an Actix Response for a Turbo Stream
22pub async fn render_turbo_stream<V, VM, E>(args: VM) -> Result<HttpResponse, E>
23where
24    V: BaseComponent,
25    V: BaseComponent<Properties = VM>,
26    VM: Send + 'static,
27{
28    let renderer = ServerRenderer::<V>::with_props(|| args);
29    let html = renderer.render().await;
30    Ok(HttpResponse::Ok()
31        .content_type("text/vnd.turbo-stream.html")
32        .body(html))
33}
34
35/// Render a Yew view to send out in an Actix Response
36/// Used when a form is not valid
37pub fn redirect<E>(path: impl Into<String>) -> Result<HttpResponse, E> {
38    let path: String = path.into();
39    Ok(HttpResponse::SeeOther()
40        .insert_header(("Location", path))
41        .finish())
42}