Add "create" template
This commit is contained in:
parent
822cf5b037
commit
56544cd0d4
6 changed files with 916 additions and 41 deletions
79
rust_web.org
79
rust_web.org
|
@ -274,7 +274,7 @@
|
|||
rocket = "0.4"
|
||||
#+END_SRC
|
||||
|
||||
**** Modify ~src/main.rs~ for Rockets "~Hello world!~" program
|
||||
**** Modify ~src/main.rs~ for Rockets "~Hello, world!~" program
|
||||
#+BEGIN_SRC rust
|
||||
#![feature(proc_macro_hygiene, decl_macro)]
|
||||
|
||||
|
@ -290,6 +290,83 @@
|
|||
}
|
||||
#+END_SRC
|
||||
|
||||
**** Add ~Rocket.toml~
|
||||
#+BEGIN_SRC toml
|
||||
[development]
|
||||
address = "0.0.0.0"
|
||||
port = 8000
|
||||
workers = 2
|
||||
keep_alive = 5
|
||||
log = "normal"
|
||||
#+END_SRC
|
||||
|
||||
**** Create the base template at ~templates/base.html.tera~
|
||||
#+BEGIN_SRC html
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<title>simple text board</title>
|
||||
</head>
|
||||
<body>
|
||||
<ul>
|
||||
<li><a href="/">home</a></li>
|
||||
<li><a href="/create">create post</a></li>
|
||||
</ul>
|
||||
{% block content %} {% endblock %}
|
||||
</body>
|
||||
</html>
|
||||
#+END_SRC
|
||||
|
||||
**** Create the create template at ~templates/create.html.tera~
|
||||
#+BEGIN_SRC html
|
||||
{% extends "base" %}
|
||||
|
||||
{% block content %}
|
||||
<h1>simple text board</h1>
|
||||
<h2>create a post</h2>
|
||||
{% if flash %}
|
||||
<p>{{ flash }}</p>
|
||||
{% endif %}
|
||||
<form action="/create" method="post" accept-charset="utf-8">
|
||||
<label for="author">author name</label>
|
||||
<input type="text" placeholder="author" name="author" required>
|
||||
|
||||
<label for="email">email</label>
|
||||
<input type="text" placeholder="email" name="email">
|
||||
|
||||
<label for="title">title</label>
|
||||
<input type="text" placeholder="title" name="title" required>
|
||||
|
||||
<label for="content">content</label>
|
||||
<textarea type="text" placeholder="content" name="content" rows="8" cols="50" required>
|
||||
</textarea>
|
||||
|
||||
<button type="submit">create</button>
|
||||
</form>
|
||||
{% endblock content %}
|
||||
#+END_SRC
|
||||
|
||||
**** Create the ~create_form~ function
|
||||
#+BEGIN_SRC rust
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rocket::{get, request::FlashMessage, routes};
|
||||
use rocket_contrib::templates::Template;
|
||||
|
||||
#[get("/create")]
|
||||
fn create_form(flash: Option<FlashMessage>) -> Template {
|
||||
let mut context = HashMap::new();
|
||||
|
||||
if let Some(ref msg) = flash {
|
||||
context.insert("flash", msg.msg());
|
||||
}
|
||||
|
||||
Template::render("create", &context)
|
||||
}
|
||||
#+END_SRC
|
||||
|
||||
* Conclusion
|
||||
|
||||
** What did we do?
|
||||
|
|
770
simple_text_board/Cargo.lock
generated
770
simple_text_board/Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -6,3 +6,8 @@ edition = "2018"
|
|||
|
||||
[dependencies]
|
||||
rocket = "0.4"
|
||||
|
||||
[dependencies.rocket_contrib]
|
||||
version = "0.4"
|
||||
default-features = false
|
||||
features = ["tera_templates"]
|
||||
|
|
|
@ -1,6 +1,20 @@
|
|||
#![feature(proc_macro_hygiene, decl_macro)]
|
||||
|
||||
use rocket::{get, routes};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rocket::{get, request::FlashMessage, routes};
|
||||
use rocket_contrib::templates::Template;
|
||||
|
||||
#[get("/create")]
|
||||
fn create_form(flash: Option<FlashMessage>) -> Template {
|
||||
let mut context = HashMap::new();
|
||||
|
||||
if let Some(ref msg) = flash {
|
||||
context.insert("flash", msg.msg());
|
||||
}
|
||||
|
||||
Template::render("create", &context)
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
fn index() -> &'static str {
|
||||
|
@ -8,5 +22,8 @@ fn index() -> &'static str {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
rocket::ignite().mount("/", routes![index]).launch();
|
||||
rocket::ignite()
|
||||
.mount("/", routes![index, create_form])
|
||||
.attach(Template::fairing())
|
||||
.launch();
|
||||
}
|
||||
|
|
15
simple_text_board/templates/base.html.tera
Normal file
15
simple_text_board/templates/base.html.tera
Normal file
|
@ -0,0 +1,15 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<title>simple text board</title>
|
||||
</head>
|
||||
<body>
|
||||
<ul>
|
||||
<li><a href="/">home</a></li>
|
||||
<li><a href="/create">create post</a></li>
|
||||
</ul>
|
||||
{% block content %} {% endblock %}
|
||||
</body>
|
||||
</html>
|
25
simple_text_board/templates/create.html.tera
Normal file
25
simple_text_board/templates/create.html.tera
Normal file
|
@ -0,0 +1,25 @@
|
|||
{% extends "base" %}
|
||||
|
||||
{% block content %}
|
||||
<h1>simple text board</h1>
|
||||
<h2>create a post</h2>
|
||||
{% if flash %}
|
||||
<p>{{ flash }}</p>
|
||||
{% endif %}
|
||||
<form action="/create" method="post" accept-charset="utf-8">
|
||||
<label for="author">author name</label>
|
||||
<input type="text" placeholder="author" name="author" required>
|
||||
|
||||
<label for="email">email</label>
|
||||
<input type="text" placeholder="email" name="email">
|
||||
|
||||
<label for="title">title</label>
|
||||
<input type="text" placeholder="title" name="title" required>
|
||||
|
||||
<label for="content">content</label>
|
||||
<textarea type="text" placeholder="content" name="content" rows="8" cols="50" required>
|
||||
</textarea>
|
||||
|
||||
<button type="submit">create</button>
|
||||
</form>
|
||||
{% endblock content %}
|
Loading…
Reference in a new issue