All Projects → maghoff → bart

maghoff / bart

Licence: MIT License
A compile time templating language for Rust inspired by Mustache

Programming Languages

rust
11053 projects
HTML
75241 projects

Projects that are alternatives of or similar to bart

gender-render
Template-system and proof-of-concept for rendering gender-neutral text-, email- and RPG-text-templates with the correct pronouns of all people involved.
Stars: ✭ 21 (-27.59%)
Mutual labels:  template-engine, template-language
Bars
Bars is a lightweight high performance HTML aware templating engine. Bars emits DOM rather than DOM-strings, this means the DOM state is preserved even if data updates happen.
Stars: ✭ 5 (-82.76%)
Mutual labels:  template-engine, template-language
Twirl
Twirl is Play's default template engine
Stars: ✭ 498 (+1617.24%)
Mutual labels:  template-engine, template-language
Tuxedo
Tuxedo is a template language for Swift.
Stars: ✭ 80 (+175.86%)
Mutual labels:  template-engine, template-language
Pongo2
Django-syntax like template-engine for Go
Stars: ✭ 2,111 (+7179.31%)
Mutual labels:  template-engine, template-language
Phptal
PHP Template Attribute Language — template engine for XSS-proof well-formed XHTML and HTML5 pages
Stars: ✭ 155 (+434.48%)
Mutual labels:  template-engine, template-language
Awesome Twig
A curated list of amazingly awesome Twig extensions, snippets and tutorials
Stars: ✭ 63 (+117.24%)
Mutual labels:  template-engine, template-language
escapevelocity
A subset reimplementation of Apache Velocity with a much simpler API.
Stars: ✭ 28 (-3.45%)
Mutual labels:  template-engine, template-language
karkas
A tiny template engine based on TypeScript
Stars: ✭ 14 (-51.72%)
Mutual labels:  template-engine, template-language
idris-tmustache
Total Logic-Less Templating Library
Stars: ✭ 12 (-58.62%)
Mutual labels:  template-engine
closet
The Web Framework for Flashcards
Stars: ✭ 45 (+55.17%)
Mutual labels:  template-language
typesafe-templates
Template engine that leverages JSX to generate JavaScript code from TypeScript code files rather than text templates.
Stars: ✭ 27 (-6.9%)
Mutual labels:  template-engine
khufu
A template language for incremental-dom or DSL for javascript views
Stars: ✭ 19 (-34.48%)
Mutual labels:  template-language
tonic
Super fast and powerful PHP template engine
Stars: ✭ 48 (+65.52%)
Mutual labels:  template-engine
lua-resty-aries
openresty and lua multi-function template
Stars: ✭ 47 (+62.07%)
Mutual labels:  template-engine
Contemplate
Contemplate: Fast, extendable object-oriented and light-weight Template Engine for PHP, Python, Node.js, Browser and XPCOM/SDK JavaScript
Stars: ✭ 15 (-48.28%)
Mutual labels:  template-engine
shaven
DOM building utility & Template engine based on JsonML + syntax sugar
Stars: ✭ 66 (+127.59%)
Mutual labels:  template-engine
view
Template Engine For AdonisJS
Stars: ✭ 13 (-55.17%)
Mutual labels:  template-engine
bh-php
PHP port of https://github.com/bem/bh. It's cool thing but better use this:
Stars: ✭ 33 (+13.79%)
Mutual labels:  template-engine
hyperviews
Template language that produces `h` output
Stars: ✭ 26 (-10.34%)
Mutual labels:  template-language

Build Status

Bart is a compile time templating language for Rust inspired by Mustache. It plays to Rust's strengths by statically compiling the template into efficient code and performing full variable resolution and type checking at compile time.

Cargo dependencies

To use Bart, add these dependencies to your Cargo.toml:

[dependencies]
bart = "0.1.4"
bart_derive = "0.1.4"

Example

Given the template file hello_world.html:

Hello {{name}}

We can write the following program:

#[macro_use] extern crate bart_derive;

#[derive(BartDisplay)]
#[template = "hello_world.html"]
struct HelloWorld<'a> {
    name: &'a str,
}

fn main() {
    print!("{}", &HelloWorld { name: "World" });
}

To compile this example program, you need to add both bart and bart_derive as dependencies in your Cargo.toml.

Running this program will output

Hello World

You can run this example by cloning this repository and executing cargo run --example hello_world.

Line by line

#[macro_use] extern crate bart_derive;

The programmer interface to Bart is the procedural macro defined in the bart_derive crate, which implements support for #[derive(BartDisplay)]. It must be added as a dependency in your Cargo.toml and referenced like above. bart_derive generates code which is dependent on the bart crate, so you also need to pull this in as a dependency.

#[derive(BartDisplay)]

Use bart_derive to generate an impl of the Display trait based on the template and struct below.

#[template = "hello_world.html"]

bart_derive will read hello_world.html and use it to generate the template rendering code. The given file name is relative to your crate root, so, for example, you have to specify #[template = "src/hello_world.html"] if you want your template to reside in the src/ directory.

It is also possible to specify the template inline with template_string: #[template_string = "Hello {{name}}"].

struct HelloWorld<'a> {
    name: &'a str,
}

Values to be interpolated in the template will be resolved from the given struct. In this case {{name}} would be resolved to the name field of this struct. Fields to be interpolated must implement the Display trait.

fn main() {
    print!("{}", &HelloWorld { name: "World" });
}

As noted above, bart_derive has now generated an impl of Display for HelloWorld. This means we can pass instances of HelloWorld to print!, write!, format! and so on. The template is rendered with the supplied data, generating Hello World to standard output.

Language reference

The Bart templating language is inspired by Mustache. (Bart is the Norwegian word for Mustache.)

The input is reproduced verbatim except for tags. Tags start with {{ and end with }}.

Interpolation

The simplest tag is the interpolation tag, which contains a data reference. For the template Hello {{name}}, {{name}} is recognized as an interpolation tag and name is resolved as a field on the given struct. This field must implement the Display trait. It is possible to use . to refer to fields in nested structs; {{name.surname}}.

Interpolation tags are HTML escaped, so for the template Hello {{name}}, if {{name}} is Bobby <tags>, the output will be Hello Bobby &lt;tags>.

Verbatim/unescaped interpolation

It is also useful to be able to deliberately include HTML content unescaped. Use triple-tags, {{{}}}, for this: Hello {{{name}}} would render Hello Bobby <tags> if name were Bobby <tags>.

Iteration

It is possible to iterate over anything that implements IntoIterator:

<ul>
{{#values}}
    <li>{{.}}</li>
{{/values}}
</ul>

Use {{.}} to refer to the current value. For example, if values were a Vec<i32>, {{.}} would refer to each of the contained i32 values in turn. When iterating over a set of structures, use a . prefix to refer to members:

<ul>
{{#people}}
    <li>{{.name}} ({{.age}})</li>
{{/people}}
</ul>

It can be useful to take advantage of the IntoIterator implementations on Option and Result to use them in Bart iterations.

Scoping

Similar to iteration, it is possible to enter a scope for a variable, by specifying a trailing dot:

{{#person.}}
    {{.name}} ({{.age}})
{{/person}}

It is also possible to fully qualify each reference:

{{person.name}} ({{person.age}})

When in a nested scope, use multiple leading dots to step out:

{{#department.}}
    {{#head.}}
        {{.name}}, head of the {{..name}} department.
    {{/head}}
{{/department}}

Unqualified names, that is, names without leading dots, will always be resolved in the topmost scope.

The same scoping rules apply to iteration scopes.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].