Skip to content

Commit

Permalink
add minijinja for templating example
Browse files Browse the repository at this point in the history
  • Loading branch information
va-an authored and the10thWiz committed Jun 26, 2024
1 parent 6857b82 commit 80844f2
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 7 deletions.
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
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"]
17 changes: 14 additions & 3 deletions examples/templating/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#[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 +24,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");
}
9 changes: 9 additions & 0 deletions examples/templating/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"); // FIXME: in progress
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>

0 comments on commit 80844f2

Please sign in to comment.