Open gurugeek opened 6 years ago
Hey @gurugeek !
I'm not sure I get what you mean exactly, but the most basic set up is this;
Build a template
let template = mustache::compile_str("hello {{name}}!").expect("Oops?");
This is a reusable rust-mustache representation of your template string
Get your data together
let mut data = HashMap::new();
data.insert("name", "gurugeek");
Render it (and print it)
let mut bytes = vec![];
template.render(&mut bytes, &data).expect("Failed to render");
let message = str::from_utf8(&bytes).unwrap();
println!("{}", message);
This prints hello gurugeek!
@ninjabear many thanks for your reply! but why on all examples and this one you build a template? don't we call the template from html ? e.g. in javascript function loadUser() {
var template = $('#template').html();
Mustache.parse(template); // optional, speeds up future uses
var rendered = Mustache.render(template, {name: "Luke"});
$('#target').html(rendered);
}
also the old rustache seemed with a more clear example on how to achieve this
rustache::render_file("path/to/template.html", data);
sorry if I am missing something obvious...
oh! to use a file you can replace what I had in 1) with something like this:
let template = mustache::compile_path("path/to/template.html").expect("Failed to get template");
I'm not sure what you mean about HTML, mustache isn't much concerned with the contents of the text files, it just treats them as mustache templates (looking for things like {{
and }}
). It could be HTML, JSON, Javascript (or anything really).
thank you @ninjabear yes I think this should really make it to the examples ! in 99% of the cases the point of mustache is to compile from a file (ok can be not just html ..but in many cases it will be) but perhaps it is already clear to the experts :) thanks again !
Yep totally see your point there @gurugeek! I'll submit a PR soon with a couple more examples
I've created https://github.com/nickel-org/rust-mustache/pull/58 with examples of compile_path
and using a template string (both using HashMap
)
@ninjabear thanks appreciated !
Hi @gurugeek,
Hopefully you've found the examples provided by @ninjabear help your use-case! I just have one note to add:
a) how to define the template directory
We don't currently support this but it wouldn't be so hard to add something like that in future if you think it would be useful. Do you have a preference for how you might use the api?
If any of the dev team needs someone to sponsor (aka pay) for the time to write them I would be happy to support the project.
Not really sure how to manage something like that, but maybe you and @ninjabear can come to an agreement if you like :)
Hi and first of all thanks for your library! I had some issue with rustache so I am happy to see that there is an alternative. This said I was wondering if you could improve the example in a way to explain a) how to define the template directory b) how to achieve something as simple as
rustache::render_file("path/to/template.html", data);
is this done by template.render ? not very clear c) call me old fashion but I plan to use this to display data fetched from a database. These would go a long way for real time use !In short (mostly my fault probably ..new to rust) from the current docs I don't really understand how to get up and running(which in mustache should be define the template, pass the data, render it).
If any of the dev team needs someone to sponsor (aka pay) for the time to write them I would be happy to support the project.