-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat examples: add rocket dynamic template (shuttle-static-folder) ex…
…ample (#38) * feat examples: add rocket static files and dynamic template example projects. * feat examples: remove unneeded static-files rocket example * examples docs: fix incorrect comment * feat examples: code now formatted with rustfmt and comment regarding serving static content
- Loading branch information
1 parent
54e3617
commit 0642385
Showing
5 changed files
with
85 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "hello-world" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
rocket = "0.5.0-rc.2" | ||
shuttle-rocket = { version = "0.14.0" } | ||
shuttle-runtime = { version = "0.14.0" } | ||
shuttle-static-folder = "0.14.0" | ||
rocket_dyn_templates = { version = "0.1.0-rc.3", features = ["handlebars"] } | ||
tokio = { version = "1.26.0" } |
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,5 @@ | ||
# Rendering Handlebars dynamic templates with Rocket | ||
|
||
This example shows how to render templated content using Handlebars templates from [rocket](https://github.com/SergioBenitez/Rocket/) and shuttle. | ||
|
||
This example is inspired by templating example from the rocket repo, to see more ways to do this checkout the [original](https://github.com/SergioBenitez/Rocket/tree/master/examples/templating). |
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 @@ | ||
name = "static-files-rocket-app" |
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,46 @@ | ||
#[macro_use] | ||
extern crate rocket; | ||
// uncomment if you also wish to serve static resources | ||
// use rocket::fs::{FileServer, relative}; | ||
use rocket::response::Redirect; | ||
use rocket_dyn_templates::{context, Template}; | ||
use std::path::PathBuf; | ||
|
||
#[get("/")] | ||
fn index() -> Redirect { | ||
Redirect::to(uri!("/", hello(name = "Your Name"))) | ||
} | ||
#[get("/hello/<name>")] | ||
pub fn hello(name: &str) -> Template { | ||
Template::render( | ||
"index", | ||
context! { | ||
title: "Hello", | ||
name: Some(name), | ||
items: vec!["Example", "List", "Of", "Five", "Items"], | ||
}, | ||
) | ||
} | ||
|
||
#[shuttle_runtime::main] | ||
async fn rocket( | ||
#[shuttle_static_folder::StaticFolder(folder = "templates")] static_folder: PathBuf, | ||
) -> shuttle_rocket::ShuttleRocket { | ||
/* The provisioned static folder template directory will not be a sub folder | ||
of the location of the executable so it is necessary to merge the | ||
template_dir setting into the configuration at runtime so that dynamic templates work. | ||
Note that shuttle does not include Rocket.toml | ||
so merging config is the preferred way to modify any settings | ||
that would otherwise be set in Rocket.toml | ||
*/ | ||
let template_dir = static_folder.to_str().unwrap(); | ||
let figment = rocket::Config::figment().merge(("template_dir", template_dir)); | ||
let rocket = rocket::custom(figment) | ||
// If you also wish to serve static content, uncomment line below and corresponding 'use' on line 4 | ||
// .mount("/", FileServer::from(relative!("templates"))) | ||
.mount("/", routes![index, hello]) | ||
.attach(Template::fairing()); | ||
|
||
Ok(rocket.into()) | ||
} |
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,21 @@ | ||
<!DOCTYPE html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Shuttle example: Rocket with Handlebars dynamic templates</title> | ||
</head> | ||
<body> | ||
<section id="hello"> | ||
<h1>Hi {{ name }}!</h1> | ||
<p>Try entering your own name in the page url to see the page content change <a href="./your_name">./hello/edit_this_part_of_url</a></p> | ||
<h3>Here are the items supplied by the vec! macro in the Template::render context:</h3> | ||
<ul> | ||
{{#each items}} | ||
<li>{{ this }}</li> | ||
{{/each}} | ||
</ul> | ||
</section> | ||
<section id="notes"> | ||
<p>Templates are evaluated on request, so if you edit a template while running locally using <code>cargo shuttle run</code> there is no need to rebuild.</p> | ||
</section> | ||
</body> | ||
</html> |