1
0
mirror of https://github.com/actix/examples synced 2025-06-28 18:00:37 +02:00

feat: Example for Sailfish template engine (#528)

Co-authored-by: aaverichev <Aleksandr.Averichev@hermesrussia.ru>
Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
averichev
2022-02-17 23:15:18 +03:00
committed by GitHub
parent d815a44ccc
commit ddb84ebf6f
10 changed files with 234 additions and 3 deletions

View File

@ -1,12 +1,16 @@
# Handlebars
This is an example of how to use Actix Web with the [Handlebars templating language](https://crates.io/crates/handlebars), which is currently the most popular crate that achieves this. After starting the server with `cargo run`, you may visit the following pages:
This is an example of how to use Actix Web with the [Handlebars](https://crates.io/crates/handlebars) templating language, which is currently the most popular crate that achieves this.
## Usage
```bash
cd template_engines/handlebars
cargo run
```
After starting the server, you may visit the following pages:
- http://localhost:8080
- http://localhost:8080/Emma/documents
- http://localhost:8080/Bob/passwords

View File

@ -9,4 +9,4 @@
<h1>{{name}} example</h1>
<p>This is an example of how to use {{name}} with Actix-Web.</p>
</body>
</html>
</html>

View File

@ -9,4 +9,4 @@
<h1>Welcome back, {{user}}</h1>
<p>Here's your {{data}}.</p>
</body>
</html>
</html>

View File

@ -0,0 +1,11 @@
[package]
name = "template_sailfish"
version = "0.1.0"
edition = "2021"
[dependencies]
actix-web = "4.0.0-rc.3"
actix-web-lab = "0.10"
env_logger = "0.9"
log = "0.4"
sailfish = "0.3"

View File

@ -0,0 +1,16 @@
# Sailfish
This is an example of how to use Actix Web with the [Sailfish](https://sailfish.netlify.app/) templating language.
## Usage
```bash
cd template_engines/sailfish
cargo run
```
After starting the server, you may visit the following pages:
- http://localhost:8080/page-1
- http://localhost:8080/page-678
- http://localhost:8080/Username

View File

@ -0,0 +1,62 @@
use actix_web::{
error, get,
middleware::{Compress, Logger},
web, App, HttpServer, Responder,
};
use actix_web_lab::respond::Html;
use sailfish::TemplateOnce;
#[derive(TemplateOnce)]
#[template(path = "actix.stpl")]
struct Greet<'a> {
name: &'a str,
}
#[derive(TemplateOnce)]
#[template(path = "page.stpl")]
struct Page<'a> {
id: &'a i32,
}
#[get("/{name}")]
async fn greet(params: web::Path<(String,)>) -> actix_web::Result<impl Responder> {
let body = Greet { name: &params.0 }
.render_once()
.map_err(error::ErrorInternalServerError)?;
Ok(Html(body))
}
#[get("/page-{id:\\d+}")]
async fn page(params: web::Path<(i32,)>) -> actix_web::Result<impl Responder> {
let body = Page { id: &params.0 }
.render_once()
.map_err(error::ErrorInternalServerError)?;
Ok(Html(body))
}
#[get("/")]
async fn hello() -> impl Responder {
Html("<p>Hello world!</p>".to_string())
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("starting HTTP server at http://localhost:8080");
HttpServer::new(|| {
App::new()
.service(hello)
.service(page)
.service(greet)
.wrap(Compress::default())
.wrap(Logger::default())
})
.bind(("127.0.0.1", 8080))?
.workers(1)
.run()
.await
}

View File

@ -0,0 +1 @@
Hello user, <%= name %>!!!

View File

@ -0,0 +1 @@
Page id is <%= id %>!!!