Passing a variable to HTML

hi!
I am trying to work with a simple web framework and I was wondering in this example
https://github.com/tomaka/rouille/blob/master/examples/simple-form.rs
how would I pass a variable to the displayed HTML ?

I did try to use some template engines with little success rustache doesn't seem to work and many others are not compiling with the current cargo.
Thanks for letting me know!

Have you tried format!()?

1 Like

It's not very useful to state that something "doesn't work". What doesn't work? What does "working" mean in this context? Did you file issues with the project so they can fix them?

Seems to work here:

extern crate rustache;

use rustache::Render;
use std::io::Cursor;

fn main() {
    let data = rustache::HashBuilder::new().insert("name", "your name");
    let mut out = Cursor::new(Vec::new());
    data.render("{{ name }}", &mut out).unwrap();
    println!("{}", String::from_utf8(out.into_inner()).unwrap());
}

Have you reported this to these crates?

Tera works:

#[macro_use]
extern crate tera;

use tera::Context;

fn main() {
    let tera = compile_templates!("templates/**/*");

    let mut context = Context::new();
    context.add("product", "milk");
    context.add("tax", &42);
    let r = tera.render("index.html", &context).unwrap();

    println!("{}", r);
}

Liquid works:

extern crate liquid;

fn main() {
    let template = liquid::ParserBuilder::with_liquid()
        .build()
        .parse("Liquid! {{num | minus: 2}}").unwrap();

    let mut globals = liquid::Object::new();
    globals.insert("num".to_owned(), liquid::Value::scalar(4f32));

    let output = template.render(&globals).unwrap();
    assert_eq!(output, "Liquid! 2".to_string());
}
1 Like

Indeed I did open an issue on each of the non-working crates. The most common reply is that it works only with nightly (.e.g Maude). Other developers have been very helpful so I am getting close to solve this issue :grinning:

Now it is not very useful to state that something "works" just because the basic example on their git works. That is outside the scope of the question that was specific to the rouille framework.

So if you don't have an answer or are unwilling to provide one please spare me the "RTFM" style comments that do not help anyone.

For the record (and for these looking for an answer and not a debate) a way to pass a simple variable using rouille as a framework and horror show as a template engine is available https://github.com/Stebalien/horrorshow-rs/issues/20

This gets to the heart of my original post — you (still) haven't stated what doesn't work. You have a piece of code that shows you how to return HTML (the original Rouille example code) and you have examples of how to use 3 different templating libraries to generate HTML; so it's not clear what is the missing piece here.

You just need to cram the output of the templating language into the appropriate Rouille function. Presumably you are having troubles with that, but what are those issues exactly?

It's not easy to provide an answer when it's very unclear what the question is.

I didn't tell you to RTFM, and I didn't intend to imply it either. I did intend to point out that "many others are not compiling" isn't useful because we don't know what other templating systems you used. I picked a few highly-used libraries and pointed out that they compile and produce output.

I apologize. I am honestly attempting to help you ask better questions, but I should recognize by now that many people take this type of feedback as a direct attack. This was not my intent.

1 Like

No need to apologise! Perhaps the frustration of trying many crates got the worse of me. I am sorry if I was rude..that wasn't my intention.

To be more specific..your post does indeed clarify this issue
https://github.com/rustache/rustache/issues/161 that I opened. Apparently nowhere in the example there was the include cursor that I will be happy to try out :slight_smile: to my defence their main example is still wrong so I hope they will fix it (https://github.com/rustache/rustache/issues/160).

Will keep trying but let me tell you that the main issue is perhaps a combination of my scarce knowledge with the awfully thin or outdated documentation that many of these crates have (of course I will be happy to help them or even sponsor some work on them if I manage to get one to work).

So far I had luck with horror show (thanks to the developers that solved the issue in no time). Liquid folks have been very prompt too but I still don't understand where I have to store the templates, call them and the format of the templates. Some more idiot proof examples that involve actually calling data (not random numbers??!!!) from a database would go a long way to help newcomers!

Maude is also very nice but unfortunately working only with nightly so I am not sure if I should switch to try it or work with what I have.

My original question was mostly related of the possibility to pass variables to the FORM directly
rouille::Response::html(FORM) but since is static I guess the answer is not, you need a template engine so I am working on it :slight_smile:

Thanks again for your help!

You can find the Cursor type by searching for it in the documentation: std - Rust

I am making progress so the rustache example works

extern crate rustache;

use rustache::Render;
use std::io::Cursor;

fn main() {
    let data = rustache::HashBuilder::new().insert("name", "your name");
    let mut out = Cursor::new(Vec::new());
    data.render("{{ name }}", &mut out).unwrap();
    println!("{}", String::from_utf8(out.into_inner()).unwrap());

how would I integrate it with the rouille html ?
rouille::Response::html(rustachetemplate) ?

In my tests I get

error[E0277]: the trait bound `std::string::String: std::convert::From<()>` is not satisfied
   --> src/main.rs:220:21
    |
220 |                     rouille::Response::html(me)
    |                     ^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<()>` is not implemented for `std::string::String`
    |

What’s the type of me? It looks like it’s the unit type (ie ()). Response::html takes something that can be converted into a String.

You likely didn’t intend to have () so this is probably a mistake (maybe you’re not returning a value somewhere).

thanks for your reply! basically is the
("{}", String::from_utf8(out.into_inner()).unwrap()); I tried to use also the
data.render("{{ name }}", &mut out).unwrap();
but both do not return a valid string that can be displayed as the HTML ...

if you have an example of how you would do it I would be very grateful :slight_smile:

String::from_utf8(out.into_inner()).unwrap() will give you a String - you can pass that to the html function.

thank you vitalyd! that works !! now if only their method rustache::render_file("path/to/template.html", data); would still work too :slight_smile: