Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improving example? #57

Open
gurugeek opened this issue Feb 19, 2018 · 8 comments
Open

improving example? #57

gurugeek opened this issue Feb 19, 2018 · 8 comments

Comments

@gurugeek
Copy link

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.

@ninjabear
Copy link
Collaborator

Hey @gurugeek !

I'm not sure I get what you mean exactly, but the most basic set up is this;

  1. Build a template

    let template = mustache::compile_str("hello {{name}}!").expect("Oops?");
    

    This is a reusable rust-mustache representation of your template string

  2. Get your data together

    let mut data = HashMap::new();
    data.insert("name", "gurugeek");
    
  3. 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!

@gurugeek
Copy link
Author

@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...

@ninjabear
Copy link
Collaborator

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).

@gurugeek
Copy link
Author

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 !

@ninjabear
Copy link
Collaborator

Yep totally see your point there @gurugeek! I'll submit a PR soon with a couple more examples

@ninjabear
Copy link
Collaborator

I've created #58 with examples of compile_path and using a template string (both using HashMap)

@gurugeek
Copy link
Author

@ninjabear thanks appreciated !

@Ryman
Copy link
Member

Ryman commented Mar 13, 2018

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants