How to use html! macro?

How to use the html! macro in rust?

Please clarify a bit, what crate are you using?

I just know that there is a html macro. but I don't know to use which crate. My intension is to pass a database variable to a html textbox. Can you help me?

Is it possible to pass database values to html text box?

Well, what are you going to do with this textbox? Will it be rendered on static page, or included into the page generated by some other tool, or?..

Using Rocket framework i have called html file in the GET method and stored data in database in POST method.

Using Rocket framework i have called html file in the GET method and stored data in database in POST method.....

How to bind the inserted database values to the HTML text box? So, that if user re-enters data it will be updated.

What do you mean by "called html file"? Is it a prepared static page? If so, you might want to read about rocket templates - I'm on mobile and can't give an exact link, but check official site for details.

The html file is a prepared static page.But I don't know how to pass data from rust to html.I tried using rocket TEMPLATE , and I got a warning when i ran the code.

The rocket launched successfully , but due to the warning the TEMPLATE didn't work

Can you perhaps just show some code or even link to your project? Or a minified one that reproduces your problem?

2 Likes

I'll send you the whole code .....just go through it once.

//up.sql

CREATE TABLE posts (
  id SERIAL PRIMARY KEY,
  username VARCHAR NOT NULL,
  pass VARCHAR NOT NULL
);

//down.sql

DROP TABLE posts;
//schema.rs
table! {
    posts (id) {
        id -> Int4,
        username -> Varchar,
        pass -> Varchar,
    }
}
//models.rs

use super::schema::*;

#[derive(Queryable)]
pub struct Post {
    pub id: i32,
    pub username: String,
    pub pass: String
}

#[derive(Insertable)]
#[table_name="posts"]
pub struct NewPost<'a> {
    pub username: &'a str,
    pub pass: &'a str
}

//lib.rs



#[macro_use]
extern crate diesel;
extern crate dotenv;

pub mod models;
pub mod schema;

use diesel::prelude::*;
use diesel::pg::PgConnection;
use dotenv::dotenv;
use std::env;
use self::models::{Post, NewPost};

pub fn establish_connection() -> PgConnection {
    dotenv().ok();

    let database_url = env::var("DATABASE_URL")
        .expect("DATABASE_URL must be set");
    PgConnection::establish(&database_url)
        .expect(&format!("Error connecting to {}", database_url))
}

pub fn create_post<'a>(conn: &PgConnection, username: &'a str, pass: &'a str) -> Post {
    use schema::posts;

    let new_post = NewPost {
        username: username,
        pass: pass,
    };

    diesel::insert_into(posts::table)
        .values(&new_post)
        .get_result(conn)
        .expect("Error saving new post")
}

//main.rs

#![feature(proc_macro_hygiene, decl_macro)]


#[macro_use] extern crate rocket;
extern crate diesel;
extern crate display;
extern crate rocket_contrib;
extern crate serde;



use std::fs::File;
use rocket::request::Form;
use self::display::*;

#[derive(FromForm)]
struct User
{
username: String,
password: String,
}

#[get("/")]
fn index() -> File {
    File::open("hello.html").expect("File not found")
}

#[post("/", data = "<userdata>")]
fn login(userdata: Form<User>) 
{
let a=&userdata.username;
let b=&userdata.password;
let connection = establish_connection();
let post = create_post(&connection, a, b);
}



fn main() {
    rocket::ignite().mount("/", routes![index,login]).launch();
}

//hello.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
<h1>Registration Form</h1>
<form method="POST">
<input type="text" name="username" placeholder="username"/>
<p><input type="password" name="password" placeholder="password" />
<p><button type="submit">submit</button>
</form>
</body>
</html>

I have provided you with all the code that I have written until now...!!!
Now I want you to help me to fetch data from DB to HTML text box.