Add "create" template

This commit is contained in:
finga 2021-01-12 02:10:48 +01:00
parent 822cf5b037
commit 56544cd0d4
6 changed files with 916 additions and 41 deletions

View file

@ -260,35 +260,112 @@
*** Steps to reproduce :noexport:
**** Create new crate
#+BEGIN_SRC sh
cargo init simple_text_board
cd simple_text_board
rustup override set nightly
cargo r
#+END_SRC
#+BEGIN_SRC sh
cargo init simple_text_board
cd simple_text_board
rustup override set nightly
cargo r
#+END_SRC
**** Setup Rocket
Add Rocket dependency to ~Cargo.toml~:
#+BEGIN_SRC toml
[dependencies]
rocket = "0.4"
#+END_SRC
#+BEGIN_SRC toml
[dependencies]
rocket = "0.4"
#+END_SRC
**** Modify ~src/main.rs~ for Rockets "~Hello world!~" program
#+BEGIN_SRC rust
#![feature(proc_macro_hygiene, decl_macro)]
**** Modify ~src/main.rs~ for Rockets "~Hello, world!~" program
#+BEGIN_SRC rust
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket;
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
fn main() {
rocket::ignite().mount("/", routes![index]).launch();
}
#+END_SRC
fn main() {
rocket::ignite().mount("/", routes![index]).launch();
}
#+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

File diff suppressed because it is too large Load diff

View file

@ -6,3 +6,8 @@ edition = "2018"
[dependencies]
rocket = "0.4"
[dependencies.rocket_contrib]
version = "0.4"
default-features = false
features = ["tera_templates"]

View file

@ -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();
}

View 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>

View 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 %}