Git Product home page Git Product logo

rustache's Introduction

Rustache Build Status

Rustache is a Rust implementation of the Mustache spec.

Documentation

The different Mustache tags are documented at the mustache(5) man page.

The project's docs page is located here.

Install

Install it through Cargo:

rustache = "^0.1"

Then link it within your crate like so:

extern crate rustache;

API Methods

The main forward interface that users will interact with when using Rustache is the render method provided by the rustache::Render trait like so:

// Renders the given template string
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());

Examples

Here's an example of how to pass in data to the render_text method using the HashBuilder:

let data = HashBuilder::new()
    .insert("name", "Bob");
let mut out = Cursor::new(Vec::new());

data.render("{{ name }}", &mut out);

Here's an example of how to pass in data in the form of a JSON enum to a render method:

let data = json::from_str(r#"{"name": "Bob"}"#);
let mut out = Cursor::new(Vec::new());

data.render("{{ name }}", &mut out);

For more examples please see the tests directory.

Testing

Simply clone and run:

cargo test

Roadmap

  • Full Mustache spec compliance.

    • Comment and Section whitespace handling
    • Handle change of delimeters
  • Thread errors through the parser and compiler:

  • Real world project examples.

  • Handle Build and Parser operations concurrently

Contribute

See CONTRIBUTING.md for guidelines on how to contribute.

License

Copyright (c) 2014 Team Rustache

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Inspired by https://github.com/erickt/rust-mustache:

Copyright (c) 2012 Erick Tryzelaar

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

rustache's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rustache's Issues

Make render_text/render_file take a pointer

Please consider the following use case:

let data = HashBuilder::new();
for foo in &vec {
    let a = rustache::render_file(foo, data);
}

The above code will trigger use of moved value: 'data'.

Please could be possible to change the signature of render_file? This will obviously break other people code, but it's an easy build fix which opens to more use cases.

Thanks

examples on main git page are not working

tried

 let data = rustache::HashBuilder::new()
    .insert("name", "Bob");
let mut out = Cursor::new(Vec::new());

data.render("{{ name }}", &mut out); 

and other examples return

error[E0433]: failed to resolve. Use of undeclared type or module `Cursor`
   --> main.rs:323:15
    |
323 | let mut out = Cursor::new(Vec::new());
    |               ^^^^^^ Use of undeclared type or module `Cursor`

insert_vector/push_vector and insert_hash/push_hash should accept FnOnce, not Fn

Currently HashBuilder::insert_{vector, hash} and VecBuilder::push_{vector, hash} accept Fn for their function argument. This disallows moving out of the builder closure environment event though it is called only once in rustache code. Therefore, it is impossible, for example, to consume a vector of Strings and use its contents directly - you always need to clone all of the strings. With FnOnce these extra allocations could be avoided.

Configuring delimiters?

Is it possible to change the default delimiters from {{ }} to a different set of characters? For example, to template a file which uses curly braces already in it's content?

tag new version and push to crates.io

Can you tag a new version and push it to crates.io? The current version doesn't build and the most recent commit in master appears to resolve the issue.

Incorrect template rendering

I have this template:

            templates.push("
                impl treasure::Model for {{ model }} {
                    fn options() -> treasure::ModelOptions {
                        return treasure::ModelOptions
                        {
                            name: \"{{model}}\",
                        }
                    }
                }
            ".to_string());

The data for template is:

                let data = rustache::HashBuilder::new()
                    .insert_string("model", "test");

Rendered template is:

                impl treasure::Model for test                               name: "test",
                        }
                    }
                }

Which is incorrect.

Add examples

There is an almost empty example dir, it would be great if it has some examples.

Implement the Clone trait for Data and HashBuilder

Passing HashBuilders around is currently very difficult as HashBuilder and Data do not implement Clone. If this could be implemented, HashBuilders could be defined once and then passed to a function multiple times.

If this is already possible in a different way, please let me know.

Unexpected write to stdout

Given a template {{#show}}{{#n}}{{/n}}{{/show}}, and some json {"show":true}, calling render_text prints show to stdout. Is this intentional? How would I suppress this?

Full code example:

extern crate rustache;

use std::io::Read;

fn main() {

    let template = r#"{{#show}}{{#n}}{{/n}}{{/show}}"#.to_string();
    let json = r#"{"show":true}"#.to_string();

    let mut s = rustache::render_text(&template, json).expect("stream");
    let mut rendered = String::new();
    s.read_to_string(&mut rendered).expect("read to string");
    println!("Rendered: '{}'", rendered);

}

Tested with:

  • stable-x86_64-apple-darwin
  • nightly-2016-08-04-x86_64-apple-darwin

"rv" should be "out" in the first example

Just a typo I spotted:

the last line in the first example reads:

println!("{}", String::from_utf8(rv.into_inner()).unwrap());

It should be:

println!("{}", String::from_utf8(out.into_inner()).unwrap());

migrate from MemStream to std::io::Cursor

The MemStream crate implements an in-memory type that supports Read, Write, and Seek. The in-memory type is serviced by a Vec<u8>. It should be possible at this point to use std::io::Cursor<Vec<u8>> to replace it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.