#template #write #path #file #t4 #t4rust

macro t4rust-derive

t4rust is a compile-time templating-engine which allows you to write plain rust code in your template

4 releases (2 breaking)

0.3.0 Oct 21, 2020
0.2.0 Feb 16, 2020
0.1.4 Feb 15, 2020
0.1.3 Mar 29, 2019

#441 in Template engine

Download history 7/week @ 2023-12-11 9/week @ 2023-12-18 7/week @ 2023-12-25 23/week @ 2024-01-08 14/week @ 2024-01-15 19/week @ 2024-01-22 14/week @ 2024-02-05 13/week @ 2024-02-12 64/week @ 2024-02-19 79/week @ 2024-02-26 35/week @ 2024-03-04 30/week @ 2024-03-11 34/week @ 2024-03-18

183 downloads per month
Used in 5 crates (3 directly)

MIT/Apache

25KB
608 lines

t4rust

dependency status

About

t4rust is a minimal templating engine, inspired by the T4 syntax.

Example

A simple example how to create a template.

use t4rust_derive::Template;

// Add this attribute to use a template
#[derive(Template)]
// Specify the path to the template file here
#[TemplatePath = "./examples/doc_example1.tt"]
// Add this attribute if you want to get debug parsing information
// This also enables writing temporary files, you might get better error messages.
//#[TemplateDebug]
struct Example {
    // Add fields to the struct you want to use in the template
    name: String,
    food: String,
    num: i32,
}

fn main() {
    // Generate your template by formating it.
    let result = format!("{}", Example { name: "Splamy".into(), food: "Cake".into(), num: 3 });
    println!("{}", result);
}

doc_example1.tt:

Hello From Template!
My Name is: <# write!(_fmt, "{}", self.name)?; #>
I like to eat <#= self.food #>.
<# for num in 0..self.num { #>Num:<#= num + 1 #>
<# } #>

Output:

Hello From Template!
My Name is: Splamy
I like to eat Cake.
Num:1
Num:2
Num:3

Syntax

You can simply write rust code within code blocks.

Code is written within <# and #> blocks. If you want to write a <# in template text without starting a code block simply write it twice: <#<#. Same goes for the #> in code blocks. You dont need to duplicate the <# within code blocks and #> not in template text blocks.

You can use <#= expr #> to print out a single expression.

Maybe you noticed the magical _fmt in the template. This variable gives you access to the formatter and e.g. enables you to write functions in your template. <# write!(_fmt, "{}", self.name)?; #> is equal to <#= self.name #>.

Warning: Make sure to never create a variable called _fmt! You will get weird compiler errors.

Features

Auto-escaping

Use the escape directive in your .tt file:

<#@ escape function="escape_html" #>`

And a function with this signature in your code:

fn escape_html(s: &str) -> String {
    todo!(); /* Your escaping code here */
}

All expression blocks (e.g. <#= self.name #>) will call the escape function before inserted.

You can redeclare this directive as many times and where you want in your template to change or disable (with function="") the escape function.

License

Licensed under either of

at your option.

Dependencies

~2MB
~47K SLoC