Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/no9/tide-handlebars
Use handlebar templates in tide
https://github.com/no9/tide-handlebars
Last synced: about 2 months ago
JSON representation
Use handlebar templates in tide
- Host: GitHub
- URL: https://github.com/no9/tide-handlebars
- Owner: No9
- License: apache-2.0
- Created: 2020-07-11T21:59:41.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-06-07T18:40:41.000Z (over 3 years ago)
- Last Synced: 2024-09-15T10:52:07.809Z (3 months ago)
- Language: Rust
- Size: 23.4 KB
- Stars: 10
- Watchers: 1
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# tide-handlebars
This crate exposes an extension trait that adds four functions to handlebars::Handlebars:
* `render_response` - Render the template and return a tide response using the template name to assume the content type
e.g. `template.html` would set the content type to be `text/html`* `render_body`- Render the template and return a tide body using the template name to assume the content type
e.g. `template` defaults the content type to be `text/plain`* `render_response_ext` - Render the template and return a tide response specifying the file extension explicitly
e.g. `"html"` would set the content type to be `text/html`* `render_body_ext` - Render the template and return a tide body using the template extension to assume the content type
## Documentation
* [Rust Documentation](https://docs.rs/tide-handlebars)
* [Examples](https://github.com/No9/tide-handlebars/blob/master/examples/)
---
---
## usage
```rust
use handlebars::Handlebars;
use std::collections::BTreeMap;
use tide_handlebars::prelude::*;struct HandlebarsEngine {
registry: Handlebars<'static>,
}#[async_std::main]
async fn main() -> tide::Result<()> {
tide::log::start();let mut engine = HandlebarsEngine {
registry: Handlebars::new(),
};engine
.registry
.register_template_file("simple.html", "./examples/templates/simple.html")
.unwrap();let mut app = tide::with_state(engine);
app.at("/:name")
.get(|req: tide::Request| async move {
let hb = &req.state().registry;
let name: String = req.param("name")?;
let mut data0 = BTreeMap::new();
data0.insert("name".to_string(), name);
Ok(hb.render_response("simple.html", &data0)?)
});app.listen("127.0.0.1:8080").await?;
Ok(())
}
```See the [main handlebars repo](https://github.com/sunng87/handlebars-rust) for full details on handlebars usage.