rouille
rouille copied to clipboard
templates using rouille::Response::html();
This might not really be an issue but here it goes: first thanks for writing rouille and providing with some working examples (a rare circumstance for rust framework). I did manage to use forms, connect to the db etc. but after trying for hours I can't figure out how to make a template working
I have tried
https://crates.io/crates/bart, rustache, handlebars pretty much every package but I can't figure out where I should use the template. Should I use rouille::Response::html(); ? that didn't really work.
Perhaps the example can specify how to use templates or I will be happy to write one as soon I figure this out! thanks
I personally like to use tera. But whatever template engine you use, you just render your template to a string and then pass that string along to Response::html
.
For example, after setting up tera
(before starting the server, similar to how the database connection is acquired in the database example),
// compile templates and initialize template engine
let mut templates = compile_templates!("templates/**/*");
templates.autoescape_on(vec!["html"]);
usage in a handler would look something like this:
let mut context = tera::Context::new();
context.add("foo", &foo);
context.add("bar", &bar);
let content = templates.render("index.html", &context).expect("render failed");
Response::html(content)
The only thing Response::html
really does it set the appropriate content type for the string you provide.
@jaemk thanks for your reply. So just to be sure I got it right how would I use the template in this quick example ` (GET) (/note/{id: i32}) => { // This route returns the content of a note, if it exists.
// Note that this code is a bit unergonomic, but this is mostly a problem with the
// database client library and not rouille itself.
// To do so, we first create a variable that will receive the content of the note.
let mut content: Option<String> = None;
// And then perform the query and write to `content`. This line can only panic if the
// SQL is malformed.
for row in &db.query("SELECT content FROM notes WHERE id = $1", &[&id]).unwrap() {
content = Some(row.get(0));
}
// If `content` is still empty at this point, this means that the note doesn't
// exist in the database. Otherwise, we return the content.
match content {
Some(content) => Response::text(content),
None => Response::empty_404(),
}
},`
or inside a route provided in the examples ?
If you're interested in using tera, I recommend looking at the main example. You could then use the snippet I previously mentioned inside the handler.
thank you @jaemk appreciated!
I personally use Maud, which has an example in its book:
#![feature(proc_macro)]
extern crate maud;
#[macro_use] extern crate rouille;
use maud::html;
use rouille::Response;
fn main() {
rouille::start_server("localhost:8000", move |request| {
router!(request,
(GET) (/{name: String}) => {
html! {
h1 { "Hello, " (name) "!" }
p "Nice to meet you!"
}
},
_ => Response::empty_404()
)
});
}
@bb010g yes Maud has a nice syntax but is working only on nightly.