Crates.io | tera-hot-reload |
lib.rs | tera-hot-reload |
version | 0.2.3 |
source | src |
created_at | 2024-09-30 17:49:52.952355 |
updated_at | 2024-10-27 18:23:02.869128 |
description | Hot Reload for Tera templates |
homepage | https://github.com/oxidlabs/tera-hot-reload |
repository | https://github.com/oxidlabs/tera-hot-reload |
max_upload_size | |
id | 1391993 |
size | 9,309 |
A Rust crate for generating Tera templates with hot reloading capabilities.
Tera is a fast, template rendering engine that allows you to separate your HTML templates from your application code. This crate provides a simple way to derive the TeraTemplate
trait for your struct types, making it easy to integrate Tera into your existing Rust project.
This hot-reload feature is not for compiling your Rust code. It is for generating and serving Tera templates and static files.
#[derive(TeraTemplate)]
macro to generate a Tera template from a simple struct typeTo add this crate to your project, run the following command:
cargo add tera-hot-reload
Make sure you also have the tera crate in your project's dependencies.
To derive a Tera template from a simple struct type, use the #[derive(TeraTemplate)]
macro:
use tera_hot_reload::TeraTemplate;
#[derive(TeraTemplate)]
#[template(path="index.html")]
struct HelloTemplate {
name: String,
greeting: String,
}
This will generate a Tera template that looks for an index.html
file in the templates
directory and renders the contents of the struct.
To create an Axum application with hot reloading, use the following code:
use axum::Router;
use std::sync::{LazyLock, RwLock};
use tera::Tera;
pub static TERA: LazyLock<RwLock<Tera>> = LazyLock::new(|| {
RwLock::new(tera::Tera::new("templates/**/*").expect("Failed to create Tera instance"))
});
#[tokio::main]
async fn main() {
// Initialize the LiveReloadLayer and the reloader
let livereload = LiveReloadLayer::new();
let reload = livereload.reloader()
let app = Router::new()
.nest_service("/", get(root))
.layer(livereload);
}
This will create an Axum application that serves a root
function with hot reloading enabled.
To watch for any folder changes in your template directory and reload them automatically when they are modified, use the following code:
// after adding the LiveReloadLayer to your Axum application
let _debouncer = watch(
move || {
let _ = TERA.write().unwrap().full_reload();
reloader.reload();
},
Duration::from_millis(10), // if you have tailwindcss and your machine is slow, you can increase this value
vec!["./templates"] // this is now listening for changes in the templates folder add any other folders you want to watch this can be your folder that holds your JS files or CSS or whatever you are serving in your app
)
To serve the application, use the following code:
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
This will start a TCP server on port 3000 that serves the Axum application.
watch()
function if usedTo contribute to this project, please create a new issue or pull request on this repository. We welcome any suggestions, bug fixes, and feature requests!
This project is licensed under the MIT license. See the LICENSE file for more information.
Where to learn more: