-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add templating example with tera (#267)
- Loading branch information
1 parent
1fb4ab3
commit 4486ba0
Showing
3 changed files
with
69 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
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,15 @@ | ||
<html> | ||
<head> | ||
<title>{{ page_title }}</title> | ||
</head> | ||
|
||
<body> | ||
<ul> | ||
{% for point in points %} | ||
<li> | ||
{{ point }} | ||
</li> | ||
{% endfor %} | ||
</ul> | ||
</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,53 @@ | ||
#![feature(async_await)] | ||
|
||
use tera::{self, compile_templates}; | ||
use tide::{self, App, Context, EndpointResult, Error}; | ||
|
||
// AppState to pass with context and will hold | ||
// the interface to the tera rendering engine | ||
struct AppState { | ||
template: tera::Tera, | ||
} | ||
|
||
// Render some data into the 'tera-hello-world.html template in examples/templates directory | ||
async fn index(ctx: Context<AppState>) -> EndpointResult { | ||
// Create the context for the template | ||
let mut context = tera::Context::new(); | ||
context.insert("page_title", "Hello from Tera templating!"); | ||
context.insert("points", &vec!["point1", "point2"]); | ||
|
||
// Render the variables into the template | ||
let s = ctx | ||
.state() | ||
.template | ||
.render("tera-hello-world.html", &context) | ||
.map_err(|err| { | ||
// Map the tera::Error into a Tide error | ||
let resp = http::Response::builder() | ||
.status(500) | ||
.body(err.description().into()) | ||
.unwrap(); | ||
Error::from(resp) | ||
})?; | ||
|
||
// Build normal response, putting the rendered string into bytes -> Body | ||
let resp = http::Response::builder() | ||
.header(http::header::CONTENT_TYPE, mime::TEXT_HTML.as_ref()) | ||
.status(http::StatusCode::OK) | ||
.body(s.as_bytes().into()) | ||
.expect("Failed to build response"); | ||
Ok(resp) | ||
} | ||
|
||
fn main() -> Result<(), std::io::Error> { | ||
let template_dir = format!("{}/examples/templates/*", env!("CARGO_MANIFEST_DIR")); | ||
|
||
let state = AppState { | ||
template: compile_templates!(&template_dir), | ||
}; | ||
|
||
let mut app = App::with_state(state); | ||
app.at("/").get(index); | ||
app.run("127.0.0.1:8000")?; | ||
Ok(()) | ||
} |