| Crates.io | cttm |
| lib.rs | cttm |
| version | 0.1.0 |
| created_at | 2025-10-16 05:20:54.442762+00 |
| updated_at | 2025-10-16 05:20:54.442762+00 |
| description | CTTM is Compile-Time Template engine |
| homepage | |
| repository | https://github.com/sharp0802/cttm |
| max_upload_size | |
| id | 1885434 |
| size | 14,824 |
CTTM is a simple yet powerful Rust library that allows you to process template files at compile time.
It converts your templates into native Rust code, eliminating runtime overhead for template parsing.
.ct extension.
These files contain a mix of plain text and embedded Rust logic.build.rs script in your project uses CTTM
to find and compile all .ct files into a single Rust module.cttm::import!() macro to include the generated module.Cargo.toml[build-dependencies]
cttm = { version = "*", features = ["build"] }
[dependencies]
cttm = "*"
build.rs// build.rs
use std::process::exit;
fn main() {
// Compile all template files ending in .ct in the "templates" directory
if let Err(e) = cttm::compile_all("templates/**/*.ct") {
eprintln!("{}", e);
exit(1);
}
}
Create a directory for your templates (e.g., templates/).
A template file consists of two parts separated by ---:
Example: templates/greeting.ct
name: &str
---
#if name == "World" {
Hello, World!
#} else {
Hi, ${name}!
#}
Example: templates/star.ct
n: usize
---
#for i in 1..=n {
${ "*".repeat(i) }
#}
In your main.rs or any other module,
import the generated template functions and call them.
// src/main.rs
use std::io::stdout;
// Import the generated module (cttm::tpl::*)
::cttm::import!();
fn main() {
// Call the greeting.ct template
cttm::tpl::greeting(&mut stdout(), "Rust").unwrap();
println!(); // for a newline
// Call the star.ct template
cttm::tpl::star(&mut stdout(), 5).unwrap();
}
The module path will be cttm::tpl::<template_name>,
where <template_name> is the filename of your template without the .ct extension.
--- declares the arguments for the generated function.# is treated as a line of Rust code (e.g., for loops, if/else conditions, variable assignments).${...} to embed the result of a Rust expression into the output.# at the beginning of a line, escape it with a backslash: \#. To print a literal $, use \$.This project is licensed under the MIT License.