-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MiniJinja for templating example (#2799)
Update guide to mention MiniJinja. Adds MiniJinja to the templating example.
- Loading branch information
Showing
11 changed files
with
137 additions
and
11 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
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
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,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"); | ||
} |
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,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> |
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,3 @@ | ||
<footer> | ||
<a href="/">Home</a> | ||
</footer> |
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,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 %} |
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,11 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Rocket Example - {{ title }}</title> | ||
</head> | ||
<body> | ||
{% include "minijinja/nav" %} | ||
{% block page %}{% endblock %} | ||
{% include "minijinja/footer" %} | ||
</body> | ||
</html> |
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 @@ | ||
<a href="/minijinja/hello/Unknown">Hello</a> | <a href="/minijinja/about">About</a> |