22 unstable releases (3 breaking)

✓ Uses Rust 2018 edition

new 0.3.5 Oct 15, 2019
0.3.3 Sep 26, 2019
0.3.0 Jul 6, 2019
0.2.0 Mar 31, 2019

#15 in Template engine

Download history 264/week @ 2019-07-02 161/week @ 2019-07-09 64/week @ 2019-07-16 56/week @ 2019-07-23 92/week @ 2019-07-30 219/week @ 2019-08-06 101/week @ 2019-08-13 125/week @ 2019-08-20 182/week @ 2019-08-27 125/week @ 2019-09-03 239/week @ 2019-09-10 281/week @ 2019-09-17 244/week @ 2019-09-24 232/week @ 2019-10-01 121/week @ 2019-10-08

689 downloads per month

MIT/Apache

28KB
629 lines

Yarte Documentation Latest version Build Status

Yarte stands for Yet Another Rust Template Engine, is the fastest template engine. Uses a Handlebars-like syntax, well known and intuitive for most developers. Yarte is an optimized, and easy-to-use rust crate, with which developers can create logic around their HTML templates using using conditionals, loops, rust code and using templates composition.

Features

  • Ignores unnecessary conditionals (if and unless) from users code.
  • Evaluation of rust expression in compile time.
  • A fancy-text debug mode to visualize the code generated by Yarte.
  • Incorporates feature with-actix-web, an implementation of actix-web's trait Responder for those using this framework.

Documentation

In order to fully understand Yarte's capabilities take a look at the following documentation:

Getting started

Follow with the help of code in example directory.

Add Yarte dependency to your Cargo.toml file:

[dependencies]
yarte = "0.3"

Yarte templates look like regular text, with embedded yarte expressions. Create a simple Yarte template called hello.html in your template directory.

<div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
</div>

In order to use a struct in a Yarte template you will have to call the procedural macro Template. For example, in the following code we are going to use struct CardTemplate, to then define template as a CardTemplate with content.

use yarte::Template;

#[derive(Template)]
#[template(path = "hello.html")]
struct CardTemplate<'a> {
    title: &'a str,
    body: &'a str,
}

Yarte will read hello.html and build a parser for the template at compile time, that can be later applied to any CardTemplate object.

let template = CardTemplate {
    title: "My Title",
    body: "My Body",
};

In this case template is an object CardTemplate correctly defined, so now template can be rendered using function self.call() and call your template to allocate the result in a String and return it wrapped with yarte::Result.

template.call()

will write in the formatter the following string:

<div class="entry">
  <h1> My Title </h1>
  <div class="body">
    My Body
  </div>
</div>

Roadmap

  • intellij plugin
  • error report
  • Minimize html5, css and js
  • ... you can open a issue!

We are not looking for anything other than render HTML5 and text as fast as possible. You can open a pull request in another case.

Acknowledgment

Yarte is based on all previous templates engines, syntax as well as its documentation is highly influenced by Handlebars. Implemented mainly with nom, memchr and syn, among others crates . As many ideas as possible used in Yarte are from other repositories. Comments in the code clarify which ideas are used, and from where.

Is it really the fastest?

See it for yourself in the benchmarks!

Contributing

Please, contribute to Yarte! The more the better! Feel free to to open an issue and/or contacting directly with the owner for any request or suggestion.

Code of conduct

This Code of Conduct is adapted from the Contributor Covenant, version 1.4, available at http://contributor-covenant.org/version/1/4

License

This project is distributed under the terms of both the Apache License (Version 2.0) and the MIT license, specified in LICENSE-APACHE and LICENSE-MIT respectively.

Dependencies

~10–14MB
~259K SLoC