A flexible template engine for Rust

Overview

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.

Comments
  • Refactor the render API to feel more native

    Refactor the render API to feel more native

    This changes the API have render take the cursor instead of having it allocate one and drop render_text and render_file in favor of the Render trait implemented on a number of types.

    opened by cardoe 6
  • Make render_text/render_file take a pointer

    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

    opened by ilpianista 6
  • Add From implementations for Data to simplify the API

    Add From implementations for Data to simplify the API

    By adding From implementations for all of the different data types allowed for the HashBuilder and VecBuilder APIs, we can vastly simplify those APIs down to basically a simple insert or push instead of an insert_string, insert_int, insert_float, etc.

    opened by jcreekmore 4
  • examples on main git page are not working

    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`
    
    
    opened by gurugeek 2
  • Drop Render for Path

    Drop Render for Path

    This API feels a bit confusing to me and it doesn't really have tests and likely has low use so remove it before the API change release that's coming up.

    opened by cardoe 2
  • (fix) Allow text to be captured after tags

    (fix) Allow text to be captured after tags

    When parsing the template, any text in a line after a tag is dropped if there is not any whitespace before it. This allows newlines in the preceding text that is captured so that it will not be thrown away. This is an implementation of #132 that also includes a test for it.

    opened by jcreekmore 2
  • insert_vector/push_vector and insert_hash/push_hash should accept FnOnce, not Fn

    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.

    opened by netvl 2
  • "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());

    opened by ayourtch 1
  • Use error-chain for generating Error boilerplate

    Use error-chain for generating Error boilerplate

    Use the error-chain machinery for generating the Error boilerplate in a standard fashion. This branch ceases the use of RustacheResult and RustacheError in favor of redefining the standard names. Additionally, it captures the actual errors behind the stream and file errors instead of just capturing the display strings. Finally, it flattens the error structure to a single level instead of having the template errors as their own type.

    opened by jcreekmore 1
  • Allow FnOnce for {insert,push}_{vector,hash}

    Allow FnOnce for {insert,push}_{vector,hash}

    By allowing FnOnce instead of just Fn, the closure can move captured variables out of the closure, allowing the passed-in vector or hash to be consumed directly without having to be cloned first. Fixes #130.

    opened by jcreekmore 1
  • tag new version and push to crates.io

    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.

    opened by cardoe 1
  • Configuring delimiters?

    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?

    opened by lukehsiao 0
  • example on https://rustache.github.io not working

    example on https://rustache.github.io not working

    error[E0425]: cannot find function render_text in module rustache --> main.rs:323:21 | 323 | rustache::render_text(template, city); | ^^^^^^^^^^^ not found in rustache

    opened by gurugeek 0
  • Implement the Clone trait for Data and HashBuilder

    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.

    opened by silversquirl 5
Releases(0.0.3)
A template engine for Rust based on Jinja2/Django

Tera Tera is a template engine inspired by Jinja2 and the Django template language. <title>{% block title %}{% endblock title %}</title> <ul> {% for u

Vincent Prouillet 2.5k Jan 1, 2023
MiniJinja is a powerful but minimal dependency template engine for Rust

MiniJinja: a powerful template engine for Rust with minimal dependencies MiniJinja is a powerful but minimal dependency template engine for Rust which

Armin Ronacher 686 Jan 5, 2023
finch - a super fast and efficient template rendering engine for node.js

finch A super fast and efficient template rendering engine for node.js, inspired by Handlebars. Usage Finch is very simple to use: Register a template

null 1 Nov 2, 2021
Balsa is a delightfully simple HTML template engine

?? balsa Balsa is a delightfully simple HTML template engine. It is designed to be used in user interfaces such as a CMS where a user needs to be able

Tyler Lafayette 1 Jan 19, 2022
A minimalist Rust WebAssembly project template

MiniWASM - A minimalist Rust WebAssembly project template This is a minimal Rust-powered WebAssembly application template. It was designed to showcase

Emil Loer 160 Jul 26, 2022
A template for a Rust-powered static-page Try Online interface

rust-tio-template A template for a Rust-powered static-page Try Online interface What is included This is an example setup that enables all of the fol

null 2 Dec 13, 2021
A "Hello, world!" template of a Rust binary crate for the ESP-IDF framework.

Rust on ESP-IDF "Hello, World" template A "Hello, world!" template of a Rust binary crate for the ESP-IDF framework. This is the crate you get when ru

Ivan Markov 140 Jan 4, 2023
A template for creating services in Rust using Axum and Prisma.

A template for creating services in Rust using Axum and Prisma. This uses the super cool Prisma Rust Client.

Aaron Leopold 6 Oct 19, 2022
✨ A perfect template for a binary rust project.

Rust Template A project template for Rust, helping to structure your projects blazingly fast ⚡ . Features ?? Code-ready for binary projects. Add amazi

bwtecode 3 Aug 21, 2022
Compiler for Jade-like template language to cito.js-based virtual dom

Marafet A very experimental DSL for creating (mostly) single page applications in HTML. It's mostly a Jade-like (or Haml-like) templating language wit

Paul Colomiets 11 Jun 25, 2020
A nice template for NEAR repos

Template for a NEAR project If you're looking for a no-std version of this template, go here. Contains: a setup script Cargo.toml setup with simulatio

Thor 12 Dec 28, 2021
Yew Tauri Desktop App Template

Yew Tauri Desktop App Template This is Template App for Desktop App. Run Develop $ cargo tauri dev Build App $ cargo tauri build Setup yew setup Using

usa 16 Oct 16, 2022
Template for Cargo based SysY compiler projects.

基于 Cargo 的 SysY 编译器项目模板 该仓库中存放了一个基于 Cargo 的 SysY 编译器项目的模板, 你可以在该模板的基础上进行进一步的开发. 该仓库中的 Rust 代码实现仅作为演示, 不代表你的编译器必须以此方式实现. 如你需要使用该模板, 建议你删掉所有 Rust 源文件, 仅

PKU Compiler Course 1 Nov 1, 2021
Bevy GitHub CI Template

Bevy GitHub CI Template This repo show how to setup CI on a github project for Bevy. It creates two workflows: CI Release CI Definition: .github/workf

MiniaczQ 2 Feb 7, 2022
This is a template repo for eframe, a framework for writing apps using egui.

eframe template This is a template repo for eframe, a framework for writing apps using egui. The goal is for this to be the simplest way to get starte

Viet Dinh 0 Feb 5, 2022
Rust templating with Handlebars

handlebars-rust Handlebars templating language implemented in Rust and for Rust. Handlebars-rust is the template engine that renders the official Rust

Ning Sun 922 Dec 27, 2022
:pencil: Compile-time HTML templates for Rust

maud Documentation (source) • API reference • Change log Maud is an HTML template engine for Rust. It's implemented as a macro, html!, which compiles

Chris Wong 1.4k Jan 1, 2023
A macro-based html builder for rust

Horrorshow A macro-based html templating library, compatible with stable rust (currently requires rust >= 1.37). Features This crate will degrade grac

Steven Allen 267 Dec 11, 2022
Rust Compiled Templates with static-file handling

Rust Compiled Templates — ructe This is my attempt at writing a HTML template system for Rust. Some inspiration comes from the scala template system u

Rasmus Kaj 337 Jan 8, 2023