Skip to content

Commit fb4b630

Browse files
authored
Add MiniJinja for templating example (rwf2#2799)
Update guide to mention MiniJinja. Adds MiniJinja to the templating example.
1 parent 6857b82 commit fb4b630

File tree

11 files changed

+137
-11
lines changed

11 files changed

+137
-11
lines changed

contrib/dyn_templates/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
This crate adds support for dynamic template rendering to Rocket. It
1111
automatically discovers templates, provides a `Responder` to render templates,
12-
and automatically reloads templates when compiled in debug mode. At present, it
13-
supports [Handlebars] and [Tera].
12+
and automatically reloads templates when compiled in debug mode. It supports [Handlebars], [Tera] and [MiniJinja].
1413

1514
[Tera]: https://docs.rs/crate/tera/1
1615
[Handlebars]: https://docs.rs/crate/handlebars/5
16+
[MiniJinja]: https://docs.rs/crate/minijinja/2.0.1
1717

1818
# Usage
1919

@@ -23,7 +23,7 @@ supports [Handlebars] and [Tera].
2323
```toml
2424
[dependencies.rocket_dyn_templates]
2525
version = "0.1.0"
26-
features = ["handlebars", "tera"]
26+
features = ["handlebars", "tera", "minijinja"]
2727
```
2828

2929
1. Write your template files in Handlebars (`.hbs`) and/or Tera (`.tera`) in

docs/guide/06-responses.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ are:
354354
* [`Flash`] - Sets a "flash" cookie that is removed when accessed.
355355
* [`Json`] - Automatically serializes values into JSON.
356356
* [`MsgPack`] - Automatically serializes values into MessagePack.
357-
* [`Template`] - Renders a dynamic template using handlebars or Tera.
357+
* [`Template`] - Renders a dynamic template using Handlebars, Tera or MiniJinja.
358358

359359
[`status`]: @api/master/rocket/response/status/
360360
[`content`]: @api/master/rocket/response/content/
@@ -589,7 +589,7 @@ reloading is disabled.
589589

590590
The [`Template`] API documentation contains more information about templates,
591591
including how to customize a template engine to add custom helpers and filters.
592-
The [templating example](@git/master/examples/templating) uses both Tera and Handlebars
592+
The [templating example](@git/master/examples/templating) uses Tera, Handlebars and MiniJinja
593593
templating to implement the same application.
594594

595595
[configurable]: ../configuration/

examples/templating/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ rocket = { path = "../../core/lib" }
1111
# in your application, you should enable only the template engine(s) used
1212
[dependencies.rocket_dyn_templates]
1313
path = "../../contrib/dyn_templates"
14-
features = ["tera", "handlebars"]
14+
features = ["tera", "handlebars", "minijinja"]

examples/templating/src/main.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
#[macro_use] extern crate rocket;
1+
#[macro_use]
2+
extern crate rocket;
23

34
mod hbs;
5+
mod minijinja;
46
mod tera;
57

6-
#[cfg(test)] mod tests;
8+
#[cfg(test)]
9+
mod tests;
710

811
use rocket::response::content::RawHtml;
912
use rocket_dyn_templates::Template;
1013

1114
#[get("/")]
1215
fn index() -> RawHtml<&'static str> {
13-
RawHtml(r#"See <a href="tera">Tera</a> or <a href="hbs">Handlebars</a>."#)
16+
RawHtml(
17+
r#"See <a href="tera">Tera</a>,
18+
<a href="hbs">Handlebars</a>,
19+
or <a href="minijinja">MiniJinja</a>."#,
20+
)
1421
}
1522

1623
#[launch]
@@ -19,10 +26,16 @@ fn rocket() -> _ {
1926
.mount("/", routes![index])
2027
.mount("/tera", routes![tera::index, tera::hello, tera::about])
2128
.mount("/hbs", routes![hbs::index, hbs::hello, hbs::about])
29+
.mount(
30+
"/minijinja",
31+
routes![minijinja::index, minijinja::hello, minijinja::about],
32+
)
2233
.register("/hbs", catchers![hbs::not_found])
2334
.register("/tera", catchers![tera::not_found])
35+
.register("/minijinja", catchers![minijinja::not_found])
2436
.attach(Template::custom(|engines| {
2537
hbs::customize(&mut engines.handlebars);
2638
tera::customize(&mut engines.tera);
39+
minijinja::customize(&mut engines.minijinja);
2740
}))
2841
}

examples/templating/src/minijinja.rs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use rocket::response::Redirect;
2+
use rocket::Request;
3+
4+
use rocket_dyn_templates::{context, minijinja::Environment, Template};
5+
6+
// use self::minijinja::;
7+
8+
#[get("/")]
9+
pub fn index() -> Redirect {
10+
Redirect::to(uri!("/minijinja", hello(name = "Your Name")))
11+
}
12+
13+
#[get("/hello/<name>")]
14+
pub fn hello(name: &str) -> Template {
15+
Template::render(
16+
"minijinja/index",
17+
context! {
18+
title: "Hello",
19+
name: Some(name),
20+
items: vec!["One", "Two", "Three"],
21+
},
22+
)
23+
}
24+
25+
#[get("/about")]
26+
pub fn about() -> Template {
27+
Template::render(
28+
"minijinja/about.html",
29+
context! {
30+
title: "About",
31+
},
32+
)
33+
}
34+
35+
#[catch(404)]
36+
pub fn not_found(req: &Request<'_>) -> Template {
37+
println!("Handling 404 for URI: {}", req.uri());
38+
39+
Template::render(
40+
"minijinja/error/404",
41+
context! {
42+
uri: req.uri()
43+
},
44+
)
45+
}
46+
47+
pub fn customize(env: &mut Environment) {
48+
env.add_template(
49+
"minijinja/about.html",
50+
r#"
51+
{% extends "minijinja/layout" %}
52+
53+
{% block page %}
54+
<section id="about">
55+
<h1>About - Here's another page!</h1>
56+
</section>
57+
{% endblock %}
58+
"#,
59+
)
60+
.expect("valid Jinja2 template");
61+
}

examples/templating/src/tests.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ fn test_404(base: &str) {
4040
let client = Client::tracked(rocket()).unwrap();
4141
for bad_path in &["/hello", "/foo/bar", "/404"] {
4242
let path = format!("/{}{}", base, bad_path);
43-
let escaped_path = RawStr::new(&path).html_escape();
43+
let escaped_path = RawStr::new(&path).html_escape().to_lowercase();
4444

4545
let response = client.get(&path).dispatch();
4646
assert_eq!(response.status(), Status::NotFound);
47-
let response = response.into_string().unwrap();
47+
let response = response.into_string().unwrap().to_lowercase();
4848

4949
assert!(response.contains(base));
5050
assert! {
@@ -66,6 +66,7 @@ fn test_index() {
6666
let response = client.get("/").dispatch().into_string().unwrap();
6767
assert!(response.contains("Tera"));
6868
assert!(response.contains("Handlebars"));
69+
assert!(response.contains("MiniJinja"));
6970
}
7071

7172
#[test]
@@ -83,3 +84,11 @@ fn tera() {
8384
test_404("tera");
8485
test_about("tera");
8586
}
87+
88+
#[test]
89+
fn minijinja() {
90+
test_root("minijinja");
91+
test_name("minijinja");
92+
test_404("minijinja");
93+
test_about("minijinja");
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>404 - minijinja</title>
6+
</head>
7+
<body>
8+
<h1>404: Hey! There's nothing here.</h1>
9+
The page at {{ uri }} does not exist!
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<footer>
2+
<a href="/">Home</a>
3+
</footer>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% extends "minijinja/layout" %}
2+
3+
{% block page %}
4+
<section id="hello">
5+
<h1>Hi {{ name }}!</h1>
6+
<h3>Here are your items:</h3>
7+
<ul>
8+
{% for item in items %}
9+
<li>{{ item }}</li>
10+
{% endfor %}
11+
</ul>
12+
</section>
13+
14+
<section id="custom-helper">
15+
<p>Try going to <a href="/minijinja/hello/Your%20Name">/minijinja/hello/Your Name</a>.</p>
16+
</section>
17+
{% endblock %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Rocket Example - {{ title }}</title>
5+
</head>
6+
<body>
7+
{% include "minijinja/nav" %}
8+
{% block page %}{% endblock %}
9+
{% include "minijinja/footer" %}
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<a href="/minijinja/hello/Unknown">Hello</a> | <a href="/minijinja/about">About</a>

0 commit comments

Comments
 (0)