Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MiniJinja for templating example #2799

Merged
merged 5 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions contrib/dyn_templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

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

[Tera]: https://docs.rs/crate/tera/1
[Handlebars]: https://docs.rs/crate/handlebars/5
[MiniJinja]: https://docs.rs/crate/minijinja/2.0.1

# Usage

Expand All @@ -23,7 +23,7 @@ supports [Handlebars] and [Tera].
```toml
[dependencies.rocket_dyn_templates]
version = "0.1.0"
features = ["handlebars", "tera"]
features = ["handlebars", "tera", "minijinja"]
```

1. Write your template files in Handlebars (`.hbs`) and/or Tera (`.tera`) in
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/06-responses.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ are:
* [`Flash`] - Sets a "flash" cookie that is removed when accessed.
* [`Json`] - Automatically serializes values into JSON.
* [`MsgPack`] - Automatically serializes values into MessagePack.
* [`Template`] - Renders a dynamic template using handlebars or Tera.
* [`Template`] - Renders a dynamic template using Handlebars, Tera or MiniJinja.

[`status`]: @api/master/rocket/response/status/
[`content`]: @api/master/rocket/response/content/
Expand Down Expand Up @@ -589,7 +589,7 @@ reloading is disabled.

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

[configurable]: ../configuration/
Expand Down
2 changes: 1 addition & 1 deletion examples/templating/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ rocket = { path = "../../core/lib" }
# in your application, you should enable only the template engine(s) used
[dependencies.rocket_dyn_templates]
path = "../../contrib/dyn_templates"
features = ["tera", "handlebars"]
features = ["tera", "handlebars", "minijinja"]
19 changes: 16 additions & 3 deletions examples/templating/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
#[macro_use] extern crate rocket;
#[macro_use]
extern crate rocket;

mod hbs;
mod minijinja;
mod tera;

#[cfg(test)] mod tests;
#[cfg(test)]
mod tests;

use rocket::response::content::RawHtml;
use rocket_dyn_templates::Template;

#[get("/")]
fn index() -> RawHtml<&'static str> {
RawHtml(r#"See <a href="tera">Tera</a> or <a href="hbs">Handlebars</a>."#)
RawHtml(
r#"See <a href="tera">Tera</a>,
<a href="hbs">Handlebars</a>,
or <a href="minijinja">MiniJinja</a>."#,
)
}

#[launch]
Expand All @@ -19,10 +26,16 @@ fn rocket() -> _ {
.mount("/", routes![index])
.mount("/tera", routes![tera::index, tera::hello, tera::about])
.mount("/hbs", routes![hbs::index, hbs::hello, hbs::about])
.mount(
"/minijinja",
routes![minijinja::index, minijinja::hello, minijinja::about],
)
.register("/hbs", catchers![hbs::not_found])
.register("/tera", catchers![tera::not_found])
.register("/minijinja", catchers![minijinja::not_found])
.attach(Template::custom(|engines| {
hbs::customize(&mut engines.handlebars);
tera::customize(&mut engines.tera);
minijinja::customize(&mut engines.minijinja);
}))
}
61 changes: 61 additions & 0 deletions examples/templating/src/minijinja.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use rocket::response::Redirect;
use rocket::Request;

use rocket_dyn_templates::{context, minijinja::Environment, Template};

// use self::minijinja::;

#[get("/")]
pub fn index() -> Redirect {
Redirect::to(uri!("/minijinja", hello(name = "Your Name")))
}

#[get("/hello/<name>")]
pub fn hello(name: &str) -> Template {
Template::render(
"minijinja/index",
context! {
title: "Hello",
name: Some(name),
items: vec!["One", "Two", "Three"],
},
)
}

#[get("/about")]
pub fn about() -> Template {
Template::render(
"minijinja/about.html",
context! {
title: "About",
},
)
}

#[catch(404)]
pub fn not_found(req: &Request<'_>) -> Template {
println!("Handling 404 for URI: {}", req.uri());

Template::render(
"minijinja/error/404",
context! {
uri: req.uri()
},
)
}

pub fn customize(env: &mut Environment) {
env.add_template(
"minijinja/about.html",
r#"
{% extends "minijinja/layout" %}

{% block page %}
<section id="about">
<h1>About - Here's another page!</h1>
</section>
{% endblock %}
"#,
)
.expect("valid Jinja2 template");
}
13 changes: 11 additions & 2 deletions examples/templating/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ fn test_404(base: &str) {
let client = Client::tracked(rocket()).unwrap();
for bad_path in &["/hello", "/foo/bar", "/404"] {
let path = format!("/{}{}", base, bad_path);
let escaped_path = RawStr::new(&path).html_escape();
let escaped_path = RawStr::new(&path).html_escape().to_lowercase();

let response = client.get(&path).dispatch();
assert_eq!(response.status(), Status::NotFound);
let response = response.into_string().unwrap();
let response = response.into_string().unwrap().to_lowercase();

assert!(response.contains(base));
assert! {
Expand All @@ -66,6 +66,7 @@ fn test_index() {
let response = client.get("/").dispatch().into_string().unwrap();
assert!(response.contains("Tera"));
assert!(response.contains("Handlebars"));
assert!(response.contains("MiniJinja"));
}

#[test]
Expand All @@ -83,3 +84,11 @@ fn tera() {
test_404("tera");
test_about("tera");
}

#[test]
fn minijinja() {
test_root("minijinja");
test_name("minijinja");
test_404("minijinja");
test_about("minijinja");
}
11 changes: 11 additions & 0 deletions examples/templating/templates/minijinja/error/404.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>404 - minijinja</title>
</head>
<body>
<h1>404: Hey! There's nothing here.</h1>
The page at {{ uri }} does not exist!
</body>
</html>
3 changes: 3 additions & 0 deletions examples/templating/templates/minijinja/footer.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<footer>
<a href="/">Home</a>
</footer>
17 changes: 17 additions & 0 deletions examples/templating/templates/minijinja/index.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "minijinja/layout" %}

{% block page %}
<section id="hello">
<h1>Hi {{ name }}!</h1>
<h3>Here are your items:</h3>
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
</section>

<section id="custom-helper">
<p>Try going to <a href="/minijinja/hello/Your%20Name">/minijinja/hello/Your Name</a>.</p>
</section>
{% endblock %}
11 changes: 11 additions & 0 deletions examples/templating/templates/minijinja/layout.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html>
<head>
<title>Rocket Example - {{ title }}</title>
</head>
<body>
{% include "minijinja/nav" %}
{% block page %}{% endblock %}
{% include "minijinja/footer" %}
</body>
</html>
1 change: 1 addition & 0 deletions examples/templating/templates/minijinja/nav.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="/minijinja/hello/Unknown">Hello</a> | <a href="/minijinja/about">About</a>
Loading