feat(tera_factory): Implement hot reload example for Tera templates with Rhai
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.
This commit is contained in:
96
tera_factory/examples/hot_reload/README.md
Normal file
96
tera_factory/examples/hot_reload/README.md
Normal file
@@ -0,0 +1,96 @@
|
||||
# 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
|
||||
<div class="greeting">{{ greet(name) }}</div>
|
||||
<p>Today's date: <span class="date">{{ format_date(current_date) }}</span></p>
|
||||
```
|
||||
|
||||
### 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.
|
Reference in New Issue
Block a user