Skip to content

Commit

Permalink
feat examples: add rocket dynamic template (shuttle-static-folder) ex…
Browse files Browse the repository at this point in the history
…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
jhawkesworth authored Apr 20, 2023
1 parent 54e3617 commit 0642385
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
12 changes: 12 additions & 0 deletions rocket/dyn_template_hbs/Cargo.toml
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" }
5 changes: 5 additions & 0 deletions rocket/dyn_template_hbs/README.md
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).
1 change: 1 addition & 0 deletions rocket/dyn_template_hbs/Shuttle.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name = "static-files-rocket-app"
46 changes: 46 additions & 0 deletions rocket/dyn_template_hbs/src/main.rs
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())
}
21 changes: 21 additions & 0 deletions rocket/dyn_template_hbs/templates/index.html.hbs
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>

0 comments on commit 0642385

Please sign in to comment.