# Tera + Rhai Hot Reload Example This example demonstrates how to use the `tera_factory` and `rhai_system` crates together to create a Tera templating engine with hot-reloadable Rhai functions. ## Overview The example shows how to: 1. Create a hot-reloadable Rhai system with multiple script files 2. Integrate the Rhai system with Tera templates 3. Automatically reload Rhai functions when script files change 4. Use Rhai functions within Tera templates ## How It Works The example uses two main components: 1. **rhai_system**: Provides hot-reloadable Rhai scripting with support for multiple script files 2. **tera_factory**: Creates a Tera engine with Rhai functions registered as Tera functions When a Rhai script file changes, the system automatically: 1. Detects the change 2. Recompiles the script 3. Updates the Tera functions with the new implementations This allows you to modify your Rhai functions at runtime and see the changes immediately in your rendered templates. ## Files - **main.rs**: The main application that sets up the hot-reloadable system and renders templates - **scripts/main.rhai**: Contains the main Rhai functions used in templates - **scripts/utils.rhai**: Contains utility Rhai functions - **templates/index.html.tera**: A Tera template that uses the Rhai functions ## Running the Example To run this example, navigate to the root of the tera_factory project and execute: ```bash cargo run --example hot_reload ``` ## What to Expect 1. The application will set up a hot-reloadable Rhai system with two script files 2. It will create a Tera engine with the Rhai functions registered 3. It will render the template every second, showing the output in the console 4. After 5 seconds, it will modify the main.rhai script with updated functions 5. The next render will automatically use the updated functions ## Key Implementation Details ### Creating a Hot-Reloadable Rhai System ```rust // Create a hot reloadable Rhai system let script_paths = vec![main_script_path.clone(), utils_script_path.clone()]; let system = Arc::new(create_hot_reloadable_system(&script_paths, None)?); // Start watching for changes to the script files system.watch(); ``` ### Creating a Tera Engine with Rhai Functions ```rust // Create a TeraFactory instance let factory = TeraFactory::new(); // Create a Tera instance with the hot reloadable Rhai system let tera = factory.create_tera_with_hot_rhai( &[template_dir.to_str().unwrap()], Arc::clone(&system) )?; ``` ### Using Rhai Functions in Tera Templates ```html
Today's date: {{ format_date(current_date) }}
``` ### Modifying Scripts at Runtime ```rust // Modify the main script after 5 seconds modify_script( &main_script_path_clone, modified_main_content, 5, "Modifying the main script with updated functions..." ); ``` This example demonstrates a powerful pattern for dynamic template rendering with hot-reloadable business logic, allowing for rapid development and testing of template functionality without restarting your application.