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:
@ -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
|
||||
|
@ -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>
|
||||
|
@ -9,4 +9,4 @@
|
||||
<h1>Welcome back, {{user}}</h1>
|
||||
<p>Here's your {{data}}.</p>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
11
template_engines/sailfish/Cargo.toml
Normal file
11
template_engines/sailfish/Cargo.toml
Normal 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"
|
16
template_engines/sailfish/README.md
Normal file
16
template_engines/sailfish/README.md
Normal 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
|
62
template_engines/sailfish/src/main.rs
Normal file
62
template_engines/sailfish/src/main.rs
Normal 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: ¶ms.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: ¶ms.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
|
||||
}
|
1
template_engines/sailfish/templates/actix.stpl
Normal file
1
template_engines/sailfish/templates/actix.stpl
Normal file
@ -0,0 +1 @@
|
||||
Hello user, <%= name %>!!!
|
1
template_engines/sailfish/templates/page.stpl
Normal file
1
template_engines/sailfish/templates/page.stpl
Normal file
@ -0,0 +1 @@
|
||||
Page id is <%= id %>!!!
|
Reference in New Issue
Block a user