This commit adds a comprehensive hot reload example that demonstrates how to use the rhai_system for dynamic template rendering with Tera. Key improvements include: - Refactor the example to use external script files instead of hardcoded Rhai code - Implement proper module imports using the BasePathModuleResolver approach - Fix template rendering by using keyword arguments in Tera function calls - Add support for hot reloading both main and utility scripts - Remove unnecessary output file generation to keep the example clean - Fix compatibility issues with Rhai functions (avoiding to_string with parameters) This example showcases how changes to Rhai scripts are automatically detected and applied to rendered templates without restarting the application, providing a smooth development experience. |
||
---|---|---|
.. | ||
scripts | ||
templates | ||
initial_main.rhai | ||
initial_utils.rhai | ||
main.rs | ||
README.md |
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:
- Create a hot-reloadable Rhai system with multiple script files
- Integrate the Rhai system with Tera templates
- Automatically reload Rhai functions when script files change
- Use Rhai functions within Tera templates
How It Works
The example uses two main components:
- rhai_system: Provides hot-reloadable Rhai scripting with support for multiple script files
- tera_factory: Creates a Tera engine with Rhai functions registered as Tera functions
When a Rhai script file changes, the system automatically:
- Detects the change
- Recompiles the script
- 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:
cargo run --example hot_reload
What to Expect
- The application will set up a hot-reloadable Rhai system with two script files
- It will create a Tera engine with the Rhai functions registered
- It will render the template every second, showing the output in the console
- After 5 seconds, it will modify the main.rhai script with updated functions
- The next render will automatically use the updated functions
Key Implementation Details
Creating a Hot-Reloadable Rhai System
// 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
// 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
<div class="greeting">{{ greet(name) }}</div>
<p>Today's date: <span class="date">{{ format_date(current_date) }}</span></p>
Modifying Scripts at Runtime
// 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.