Skip to content

Commit 0e61158

Browse files
authored
chore(examples): add rinja template (#148)
1 parent 99c42ac commit 0e61158

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

examples/templates/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
* [maud](maud)
1111
* [minijinja](minijinja)
1212
* [tera](tera)
13+
* [rinja](rinja)
1314

1415
[Here]: https://github.com/rosetta-rs/template-benchmarks-rs

examples/templates/rinja/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "rinja-template-example"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
publish = false
6+
7+
[dependencies]
8+
viz.workspace = true
9+
10+
serde.workspace = true
11+
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
12+
13+
rinja = { version = "0.3" }

examples/templates/rinja/src/main.rs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#![allow(clippy::unused_async)]
2+
3+
use std::net::SocketAddr;
4+
5+
use rinja::Template;
6+
use serde::Serialize;
7+
use tokio::net::TcpListener;
8+
use viz::{serve, Error, Request, Response, ResponseExt, Result, Router};
9+
10+
#[allow(dead_code)]
11+
#[derive(Template)]
12+
#[template(path = "index.html")]
13+
struct Tmpl<'a> {
14+
title: &'a str,
15+
users: Vec<User<'a>>,
16+
}
17+
18+
#[derive(Serialize)]
19+
struct User<'a> {
20+
url: &'a str,
21+
username: &'a str,
22+
}
23+
24+
async fn index(_: Request) -> Result<Response> {
25+
let body = Tmpl {
26+
title: "Viz.rs",
27+
users: vec![
28+
User {
29+
url: "https://github.com/rust-lang",
30+
username: "rust-lang",
31+
},
32+
User {
33+
url: "https://github.com/viz-rs",
34+
username: "viz-rs",
35+
},
36+
],
37+
}
38+
.render()
39+
.map_err(Error::boxed)?;
40+
41+
Ok(Response::html(body))
42+
}
43+
44+
#[tokio::main]
45+
async fn main() -> Result<()> {
46+
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
47+
let listener = TcpListener::bind(addr).await?;
48+
println!("listening on http://{addr}");
49+
50+
let app = Router::new().get("/", index);
51+
52+
if let Err(e) = serve(listener, app).await {
53+
println!("{e}");
54+
}
55+
56+
Ok(())
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% extends "layout.html" %}
2+
{% block title %} Viz & Rinja {% endblock %}
3+
{% block body %}
4+
<ul>
5+
{% for user in users %}
6+
<li><a href="{{ user.url }}">{{ user.username }}</a></li>
7+
{% endfor %}
8+
</ul>
9+
{% endblock %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<html>
2+
<head>
3+
<title>
4+
{% block title %}{% endblock %}
5+
</title>
6+
</head>
7+
<body>
8+
{% block body %}{% endblock %}
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)