#template #compile-time #tera #tera-templates #file #check #macro

macro file_check_macro

A macro for checking the presence of a template file at compile time. E.g. for Tera templates

1 unstable release

0.1.0 Nov 27, 2023

#1850 in Procedural macros

21 downloads per month

MIT/Apache

3KB

file_check_macro

Use this macro to get compile time errors if a template doesn't exist.

The macro in this crate, generate_template, takes a string slice (&str). It checks that the file in the string exists on disk in src, and generates a const TEMPLATE:&str = <your string slice;>.

Usage:

This:

generate_template!("path/to/my/template.tera");

Is the same as:

const TEMPLATE:&str = "path/to/my/template.tera";

But with a check that the file exists at:

src/path/to/my/template.tera

Note the prepended src.

The use case is this:

  1. You're generating HTML on the server using, say, Tera.
  2. You're a sensible person who knows that files that change together should live together. So you want to store your templates alongside their Rust handlers, not in some separate templates tree like some kind of blithering idiot.
  3. You love that rusts checks everything, and you hate run-time errors.

Dependencies

~320–770KB
~18K SLoC