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:
31
tera_factory/examples/hot_reload/scripts/initial_main.rhai
Normal file
31
tera_factory/examples/hot_reload/scripts/initial_main.rhai
Normal file
@@ -0,0 +1,31 @@
|
||||
// Main functions for Tera templates - INITIAL VERSION
|
||||
|
||||
// Format a greeting with the current version
|
||||
fn greet(name) {
|
||||
return `Hello, ${name}! Welcome to our website.`;
|
||||
}
|
||||
|
||||
// Format a product display with basic formatting
|
||||
fn format_product(name, price, discount) {
|
||||
// Use the calculate_price function from utils
|
||||
let final_price = calculate_price(price, discount);
|
||||
return `${name} - Price: $${price}, Discounted: $${final_price}`;
|
||||
}
|
||||
|
||||
// Format a list of items as HTML
|
||||
fn format_list_items(items) {
|
||||
let result = "<ul>";
|
||||
for item in items {
|
||||
result += `<li>${item}</li>`;
|
||||
}
|
||||
result += "</ul>";
|
||||
return result;
|
||||
}
|
||||
|
||||
// Import utility functions
|
||||
import "utils" as utils;
|
||||
|
||||
// Helper function that calls the utils function
|
||||
fn calculate_price(price, discount_percent) {
|
||||
return utils::calculate_price(price, discount_percent);
|
||||
}
|
22
tera_factory/examples/hot_reload/scripts/initial_utils.rhai
Normal file
22
tera_factory/examples/hot_reload/scripts/initial_utils.rhai
Normal file
@@ -0,0 +1,22 @@
|
||||
// Utility functions for Tera templates - INITIAL VERSION
|
||||
|
||||
// Format a date string
|
||||
fn format_date(date_str) {
|
||||
let parts = date_str.split("-");
|
||||
let year = parts[0];
|
||||
let month = parts[1];
|
||||
let day = parts[2];
|
||||
|
||||
return `${month}/${day}/${year}`;
|
||||
}
|
||||
|
||||
// Calculate a price with discount
|
||||
fn calculate_price(price, discount_percent) {
|
||||
let discount = price * (discount_percent / 100.0);
|
||||
return price - discount;
|
||||
}
|
||||
|
||||
// Format a currency value
|
||||
fn format_currency(amount) {
|
||||
return "$" + amount;
|
||||
}
|
32
tera_factory/examples/hot_reload/scripts/main.rhai
Normal file
32
tera_factory/examples/hot_reload/scripts/main.rhai
Normal file
@@ -0,0 +1,32 @@
|
||||
// Main functions for Tera templates - UPDATED VERSION
|
||||
|
||||
// Format a greeting with the current version
|
||||
fn greet(name) {
|
||||
return `Hello, ${name}! Welcome to our UPDATED website. (Version 2.0)`;
|
||||
}
|
||||
|
||||
// Format a product display with enhanced formatting
|
||||
fn format_product(name, price, discount) {
|
||||
// Use the calculate_price function from utils
|
||||
let final_price = calculate_price(price, discount);
|
||||
let savings = price - final_price;
|
||||
return `${name} - Original: $${price}, Now: $${final_price} (Save $${savings})`;
|
||||
}
|
||||
|
||||
// Format a list of items as HTML with CSS classes
|
||||
fn format_list_items(items) {
|
||||
let result = "<ul class='category-list'>";
|
||||
for item in items {
|
||||
result += `<li class='category-item'>${item}</li>`;
|
||||
}
|
||||
result += "</ul>";
|
||||
return result;
|
||||
}
|
||||
|
||||
// Import utility functions
|
||||
import "utils" as utils;
|
||||
|
||||
// Helper function that calls the utils function
|
||||
fn calculate_price(price, discount_percent) {
|
||||
return utils::calculate_price(price, discount_percent);
|
||||
}
|
32
tera_factory/examples/hot_reload/scripts/modified_main.rhai
Normal file
32
tera_factory/examples/hot_reload/scripts/modified_main.rhai
Normal file
@@ -0,0 +1,32 @@
|
||||
// Main functions for Tera templates - UPDATED VERSION
|
||||
|
||||
// Format a greeting with the current version
|
||||
fn greet(name) {
|
||||
return `Hello, ${name}! Welcome to our UPDATED website. (Version 2.0)`;
|
||||
}
|
||||
|
||||
// Format a product display with enhanced formatting
|
||||
fn format_product(name, price, discount) {
|
||||
// Use the calculate_price function from utils
|
||||
let final_price = calculate_price(price, discount);
|
||||
let savings = price - final_price;
|
||||
return `${name} - Original: $${price}, Now: $${final_price} (Save $${savings})`;
|
||||
}
|
||||
|
||||
// Format a list of items as HTML with CSS classes
|
||||
fn format_list_items(items) {
|
||||
let result = "<ul class='category-list'>";
|
||||
for item in items {
|
||||
result += `<li class='category-item'>${item}</li>`;
|
||||
}
|
||||
result += "</ul>";
|
||||
return result;
|
||||
}
|
||||
|
||||
// Import utility functions
|
||||
import "utils" as utils;
|
||||
|
||||
// Helper function that calls the utils function
|
||||
fn calculate_price(price, discount_percent) {
|
||||
return utils::calculate_price(price, discount_percent);
|
||||
}
|
27
tera_factory/examples/hot_reload/scripts/modified_utils.rhai
Normal file
27
tera_factory/examples/hot_reload/scripts/modified_utils.rhai
Normal file
@@ -0,0 +1,27 @@
|
||||
// Utility functions for Tera templates - UPDATED VERSION
|
||||
|
||||
// Format a date string with improved formatting
|
||||
fn format_date(date_str) {
|
||||
let parts = date_str.split("-");
|
||||
let year = parts[0];
|
||||
let month = parts[1];
|
||||
let day = parts[2];
|
||||
|
||||
// Add month names for better readability
|
||||
let month_names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
||||
let month_idx = month.parse_int() - 1;
|
||||
let month_name = month_names[month_idx];
|
||||
|
||||
return `${month_name} ${day}, ${year}`;
|
||||
}
|
||||
|
||||
// Calculate a price with discount - enhanced with minimum discount
|
||||
fn calculate_price(price, discount_percent) {
|
||||
let discount = price * (discount_percent / 100.0);
|
||||
return price - discount;
|
||||
}
|
||||
|
||||
// Format a currency value with improved formatting
|
||||
fn format_currency(amount) {
|
||||
return "$" + amount;
|
||||
}
|
27
tera_factory/examples/hot_reload/scripts/utils.rhai
Normal file
27
tera_factory/examples/hot_reload/scripts/utils.rhai
Normal file
@@ -0,0 +1,27 @@
|
||||
// Utility functions for Tera templates - UPDATED VERSION
|
||||
|
||||
// Format a date string with improved formatting
|
||||
fn format_date(date_str) {
|
||||
let parts = date_str.split("-");
|
||||
let year = parts[0];
|
||||
let month = parts[1];
|
||||
let day = parts[2];
|
||||
|
||||
// Add month names for better readability
|
||||
let month_names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
||||
let month_idx = month.parse_int() - 1;
|
||||
let month_name = month_names[month_idx];
|
||||
|
||||
return `${month_name} ${day}, ${year}`;
|
||||
}
|
||||
|
||||
// Calculate a price with discount - enhanced with minimum discount
|
||||
fn calculate_price(price, discount_percent) {
|
||||
let discount = price * (discount_percent / 100.0);
|
||||
return price - discount;
|
||||
}
|
||||
|
||||
// Format a currency value with improved formatting
|
||||
fn format_currency(amount) {
|
||||
return "$" + amount;
|
||||
}
|
Reference in New Issue
Block a user