#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

5 unstable releases

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

#371 in Template engine

Download history 54/week @ 2024-08-26 20/week @ 2024-09-02 79/week @ 2024-09-09 33/week @ 2024-09-16 83/week @ 2024-09-23 43/week @ 2024-09-30 214/week @ 2024-10-07 8/week @ 2024-10-14 11/week @ 2024-10-21 4/week @ 2024-10-28 56/week @ 2024-11-04 9/week @ 2024-11-18 36/week @ 2024-11-25 41/week @ 2024-12-02 61/week @ 2024-12-09

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

MIT/Apache

26KB
614 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

~1–1.7MB
~34K SLoC