-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(examples): add rinja template (#148)
- Loading branch information
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "rinja-template-example" | ||
version = "0.1.0" | ||
edition.workspace = true | ||
publish = false | ||
|
||
[dependencies] | ||
viz.workspace = true | ||
|
||
serde.workspace = true | ||
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } | ||
|
||
rinja = { version = "0.3" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#![allow(clippy::unused_async)] | ||
|
||
use std::net::SocketAddr; | ||
|
||
use rinja::Template; | ||
use serde::Serialize; | ||
use tokio::net::TcpListener; | ||
use viz::{serve, Error, Request, Response, ResponseExt, Result, Router}; | ||
|
||
#[allow(dead_code)] | ||
#[derive(Template)] | ||
#[template(path = "index.html")] | ||
struct Tmpl<'a> { | ||
title: &'a str, | ||
users: Vec<User<'a>>, | ||
} | ||
|
||
#[derive(Serialize)] | ||
struct User<'a> { | ||
url: &'a str, | ||
username: &'a str, | ||
} | ||
|
||
async fn index(_: Request) -> Result<Response> { | ||
let body = Tmpl { | ||
title: "Viz.rs", | ||
users: vec![ | ||
User { | ||
url: "https://github.com/rust-lang", | ||
username: "rust-lang", | ||
}, | ||
User { | ||
url: "https://github.com/viz-rs", | ||
username: "viz-rs", | ||
}, | ||
], | ||
} | ||
.render() | ||
.map_err(Error::boxed)?; | ||
|
||
Ok(Response::html(body)) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); | ||
let listener = TcpListener::bind(addr).await?; | ||
println!("listening on http://{addr}"); | ||
|
||
let app = Router::new().get("/", index); | ||
|
||
if let Err(e) = serve(listener, app).await { | ||
println!("{e}"); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% extends "layout.html" %} | ||
{% block title %} Viz & Rinja {% endblock %} | ||
{% block body %} | ||
<ul> | ||
{% for user in users %} | ||
<li><a href="{{ user.url }}">{{ user.username }}</a></li> | ||
{% endfor %} | ||
</ul> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<html> | ||
<head> | ||
<title> | ||
{% block title %}{% endblock %} | ||
</title> | ||
</head> | ||
<body> | ||
{% block body %}{% endblock %} | ||
</body> | ||
</html> |