Skip to content

Commit 1da1b90

Browse files
Update README.md
1 parent 5c8bc83 commit 1da1b90

File tree

1 file changed

+91
-55
lines changed

1 file changed

+91
-55
lines changed

rust/README.md

Lines changed: 91 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,113 @@
22

33
To use WebForms Core, first copy the WebForms class file in this directory to your project. Then create a new View file similar to the one below.
44

5-
```rust
6-
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
7-
use actix_web::middleware::Logger;
8-
9-
#[derive(Debug, Deserialize)]
10-
struct FormData {
11-
txt_name: String,
12-
txt_backgroundcolor: String,
13-
txt_fontsize: i32,
14-
btn_setbodyvalue: Option<String>,
15-
}
5+
Place the following view in the "templates" directory in the project path.
166

17-
async fn index() -> HttpResponse {
18-
let html = r#"
19-
<!DOCTYPE html>
20-
<html>
21-
<head>
22-
<title>Using WebForms Core</title>
23-
<script type="text/javascript" src="/script/web-forms.js"></script>
24-
</head>
25-
<body>
26-
<form method="post" action="/submit" >
27-
<label for="txt_Name">Your Name</label>
28-
<input name="txt_Name" id="txt_Name" type="text" />
29-
<br>
30-
<label for="txt_FontSize">Set Font Size</label>
31-
<input name="txt_FontSize" id="txt_FontSize" type="number" value="16" min="10" max="36" />
32-
<br>
33-
<label for="txt_BackgroundColor">Set Background Color</label>
34-
<input name="txt_BackgroundColor" id="txt_BackgroundColor" type="text" />
35-
<br>
36-
<input name="btn_SetBodyValue" type="submit" value="Click to send data" />
37-
</form>
38-
<body>
39-
</html>
40-
"#;
41-
42-
HttpResponse::Ok()
43-
.content_type("text/html")
44-
.body(html)
45-
}
7+
View file (index.html)
8+
```html
9+
<!DOCTYPE html>
10+
<html>
11+
<head>
12+
<title>Using WebForms Core</title>
13+
<script type="text/javascript" src="/static/script/web-forms.js"></script>
14+
</head>
15+
<body>
16+
<form method="post" action="/" >
17+
<label for="txt_Name">Your Name</label>
18+
<input name="txt_Name" id="txt_Name" type="text" />
19+
<br>
20+
<label for="txt_FontSize">Set Font Size</label>
21+
<input name="txt_FontSize" id="txt_FontSize" type="number" value="16" min="10" max="36" />
22+
<br>
23+
<label for="txt_BackgroundColor">Set Background Color</label>
24+
<input name="txt_BackgroundColor" id="txt_BackgroundColor" type="text" />
25+
<br>
26+
<input name="btn_SetBodyValue" type="submit" value="Click to send data" />
27+
</form>
28+
</body>
29+
</html>
30+
```
4631

47-
async fn submit_form(form: web::Form<FormData>, web_form: web::Data<WebForms>) -> impl Responder {
48-
let name = &form.txt_name;
49-
let background_color = &form.txt_backgroundcolor;
50-
let font_size = form.txt_fontsize;
32+
Also, create a Rust class file as follows.
5133

52-
web_form.set_font_size(InputPlace::tag("form"), font_size);
53-
web_form.set_background_color(InputPlace::tag("form"), background_color.clone());
54-
web_form.set_disabled(InputPlace::name("btn_SetBodyValue"), true);
55-
web_form.add_tag(InputPlace::tag("form"), "h3");
56-
web_form.set_text(InputPlace::tag("h3"), format!("Welcome {}!", name));
34+
Rust code
35+
```rust
36+
use actix_web::{web, App, HttpServer, HttpResponse, Responder};
37+
use tera::Tera;
38+
use std::sync::Arc;
39+
use actix_files as fs;
5740

58-
HttpResponse::Ok().body(web_form.response())
41+
mod web_forms;
42+
use crate::web_forms::{WebForms, InputPlace};
43+
44+
#[derive(Clone)]
45+
struct AppState {
46+
tera: Arc<Tera>,
5947
}
6048

6149
#[actix_web::main]
6250
async fn main() -> std::io::Result<()> {
63-
let web_form = WebForms::new();
51+
let tera = Tera::new("templates/**/*").unwrap();
52+
let state = AppState {
53+
tera: Arc::new(tera),
54+
};
6455

6556
HttpServer::new(move || {
57+
let state = state.clone();
6658
App::new()
67-
.app_data(web::Data::new(web_form.clone()))
68-
.wrap(Logger::default())
59+
.app_data(web::Data::new(state))
6960
.route("/", web::get().to(index))
70-
.route("/submit", web::post().to(submit_form))
61+
.route("/", web::post().to(handle_post))
62+
.service(fs::Files::new("/static", "./static").show_files_listing())
7163
})
7264
.bind("127.0.0.1:8080")?
7365
.run()
7466
.await
7567
}
68+
69+
async fn index(state: web::Data<AppState>) -> impl Responder {
70+
let rendered = state.tera.render("index.html", &tera::Context::new()).unwrap();
71+
HttpResponse::Ok().content_type("text/html").body(rendered)
72+
}
73+
74+
async fn handle_post(params: web::Form<Params>, state: web::Data<AppState>) -> impl Responder {
75+
let name = &params.txt_Name;
76+
let background_color = &params.txt_BackgroundColor;
77+
let font_size: i32 = params.txt_FontSize.parse().unwrap_or(16);
78+
79+
let mut form = WebForms::new();
80+
81+
form.set_font_size(InputPlace::tag("form").as_str(), font_size);
82+
form.set_background_color(InputPlace::tag("form").as_str(), background_color.clone());
83+
form.set_disabled(InputPlace::name("btn_SetBodyValue").as_str(), true);
84+
85+
form.add_tag(InputPlace::tag("form").as_str(), "h3".to_string(), "ID".to_string());
86+
form.set_text(InputPlace::tag("h3").as_str(), format!("Welcome {}!", name.to_string()));
87+
88+
return HttpResponse::Ok().body(form.response());
89+
}
90+
91+
#[derive(serde::Deserialize)]
92+
struct Params {
93+
txt_Name: String,
94+
txt_FontSize: String,
95+
txt_BackgroundColor: String,
96+
}
97+
```
98+
99+
The settings of the "Cargo.toml" file are as follows.
100+
```toml
101+
[package]
102+
name = "web_forms_core"
103+
version = "0.1.0"
104+
edition = "2025"
105+
106+
[dependencies]
107+
actix-web = "4"
108+
actix-rt = "2"
109+
tera = "1.14"
110+
serde = { version = "1.0", features = ["derive"] }
111+
actix-files = "0.6"
76112
```
77113

78114
In the upper part of the View file, it is first checked whether the submit button has been clicked or not, if it has been clicked, an instance of the WebForms class is created, then the WebForms methods are called, and then the response method is printed on the screen, and other parts Views are not displayed.
@@ -82,4 +118,4 @@ As you can see, the WebFormsJS script has been added in the header section of th
82118

83119
The latest version of the WebFormsJS script is available through the link below.
84120

85-
https://github.com/elanatframework/Web_forms/blob/elanat_framework/web-forms.js
121+
https://github.com/elanatframework/Web_forms/blob/elanat_framework/web-forms.js

0 commit comments

Comments
 (0)