tide-jsx v0.4.0
git clone https://git.pyrossh.dev/tide-jsx
Tide + JSX
README.md
| a2a8cc9 | 1 | # tide-jsx [](https://github.com/pyrossh/tide-jsx/actions?query=workflow%3ATest) [](https://crates.io/crates/tide-jsx) |
| b03ce49 | 2 | |
| a2a8cc9 | 3 | This crate allows to use jsx like templating engine (which is a slightly modified version of [render.rs](https://github.com/render-rs/render.rs)) as templates in your tide request handlers. |
| aeeb835 | 4 | Thanks to [@Schniz](https://github.com/Schniz) and all contributors for render.rs. |
| b03ce49 | 5 | |
| a2a8cc9 | 6 | ## Example |
| b03ce49 | 7 | |
| b03ce49 | 8 | ```rust |
| a2a8cc9 | 9 | use tide::http::mime; |
| a2a8cc9 | 10 | use tide::utils::After; |
| a2a8cc9 | 11 | use tide::{log, Request, Response}; |
| a2a8cc9 | 12 | use tide_jsx::html::HTML5Doctype; |
| a2a8cc9 | 13 | use tide_jsx::{component, rsx, view}; |
| b03ce49 | 14 | |
| b03ce49 | 15 | #[component] |
| b03ce49 | 16 | fn Heading<'title>(title: &'title str) { |
| a2a8cc9 | 17 | rsx! { <h1 class={"title"}>{title}</h1> } |
| b03ce49 | 18 | } |
| b03ce49 | 19 | |
| a2a8cc9 | 20 | async fn index(_req: Request<()>) -> tide::Result { |
| a2a8cc9 | 21 | view! { |
| a2a8cc9 | 22 | <> |
| a2a8cc9 | 23 | <HTML5Doctype /> |
| a2a8cc9 | 24 | <html> |
| a2a8cc9 | 25 | <head><title>{"Tide JSX"}</title></head> |
| a2a8cc9 | 26 | <body> |
| a2a8cc9 | 27 | <div> |
| a2a8cc9 | 28 | <Heading title={"Hello world"} /> |
| a2a8cc9 | 29 | </div> |
| a2a8cc9 | 30 | </body> |
| a2a8cc9 | 31 | </html> |
| a2a8cc9 | 32 | </> |
| 377f310 | 33 | } |
| 377f310 | 34 | } |
| 0c77270 | 35 | |
| a2a8cc9 | 36 | #[async_std::main] |
| a2a8cc9 | 37 | async fn main() -> tide::Result<()> { |
| a2a8cc9 | 38 | log::start(); |
| a2a8cc9 | 39 | let mut app = tide::new(); |
| a2a8cc9 | 40 | app.with(tide::log::LogMiddleware::new()); |
| a2a8cc9 | 41 | app.with(After(|mut res: Response| async { |
| a2a8cc9 | 42 | if let Some(err) = res.error() { |
| a2a8cc9 | 43 | let msg = format!("<h1>Error: {:?}</h1>", err); |
| a2a8cc9 | 44 | res.set_status(err.status()); |
| a2a8cc9 | 45 | res.set_content_type(mime::HTML); |
| a2a8cc9 | 46 | res.set_body(msg); |
| a2a8cc9 | 47 | } |
| a2a8cc9 | 48 | Ok(res) |
| a2a8cc9 | 49 | })); |
| a2a8cc9 | 50 | app.at("/").get(index); |
| a2a8cc9 | 51 | app.listen("127.0.0.1:5000").await?; |
| a2a8cc9 | 52 | Ok(()) |
| a2a8cc9 | 53 | } |
| 377f310 | 54 | ``` |