...
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
# Product Catalog
|
||||
|
||||
{% let products = get_products() %}
|
||||
|
||||
## All Products
|
||||
|
||||
{% for product in products %}
|
||||
### {{ product.name }}
|
||||
|
||||
**Price:** {{ format_price(product.price) }}
|
||||
|
||||
#### Features:
|
||||
{% for feature in product.features %}
|
||||
- {{ feature }}
|
||||
{% endfor %}
|
||||
|
||||
*Status: {% if product.available %}✅ In Stock{% else %}❌ Out of Stock{% endif %}*
|
||||
|
||||
---
|
||||
{% endfor %}
|
||||
|
||||
## Available Products
|
||||
|
||||
{% let available_products = get_available_products() %}
|
||||
*Available Products: {{ available_products.len() }}*
|
||||
|
||||
{% for product in available_products %}
|
||||
### {{ product.name }}
|
||||
|
||||
**Price:** {{ format_price(product.price) }}
|
||||
|
||||
---
|
||||
{% endfor %}
|
||||
|
||||
## Total Catalog Value
|
||||
|
||||
**Total Value:** {{ format_price(calculate_total_price()) }}
|
||||
|
||||
## User Information
|
||||
|
||||
{% let user = get_user() %}
|
||||
|
||||
**Name:** {{ user.name }}
|
||||
**Email:** {{ user.email }}
|
||||
**Role:** {{ user.role }}
|
||||
**Theme:** {{ user.settings.theme }}
|
||||
|
||||
### Order History
|
||||
|
||||
{% for order in user.orders %}
|
||||
- **{{ order.id }}** ({{ order.date }}) - {{ format_price(order.total) }}
|
||||
{% endfor %}
|
||||
|
||||
## All Product Features
|
||||
|
||||
{% let features = get_all_features() %}
|
||||
{% for feature in features %}
|
||||
- {{ feature }}
|
||||
{% endfor %}
|
2
rhai_engine/examples/simple/producttemplates/readme.md
Normal file
2
rhai_engine/examples/simple/producttemplates/readme.md
Normal file
@@ -0,0 +1,2 @@
|
||||
the data objects which need to be filled into product_catalog.tera come from ../scripts/
|
||||
|
@@ -0,0 +1,146 @@
|
||||
// Data objects for Tera template integration
|
||||
// This script defines complex objects that can be accessed from Tera templates
|
||||
|
||||
// Create a products catalog object
|
||||
fn create_products() {
|
||||
let products = [
|
||||
#{
|
||||
id: 1,
|
||||
name: "Laptop",
|
||||
price: 1299.99,
|
||||
features: [
|
||||
"16GB RAM",
|
||||
"512GB SSD",
|
||||
"Intel i7 processor"
|
||||
],
|
||||
available: true
|
||||
},
|
||||
#{
|
||||
id: 2,
|
||||
name: "Smartphone",
|
||||
price: 899.99,
|
||||
features: [
|
||||
"6.7 inch display",
|
||||
"128GB storage",
|
||||
"12MP camera"
|
||||
],
|
||||
available: true
|
||||
},
|
||||
#{
|
||||
id: 3,
|
||||
name: "Tablet",
|
||||
price: 499.99,
|
||||
features: [
|
||||
"10.9 inch display",
|
||||
"64GB storage",
|
||||
"A14 chip"
|
||||
],
|
||||
available: false
|
||||
},
|
||||
#{
|
||||
id: 4,
|
||||
name: "Headphones",
|
||||
price: 249.99,
|
||||
features: [
|
||||
"Noise cancellation",
|
||||
"Wireless",
|
||||
"20h battery life"
|
||||
],
|
||||
available: true
|
||||
}
|
||||
];
|
||||
|
||||
products
|
||||
}
|
||||
|
||||
// Get all products
|
||||
fn get_products() {
|
||||
create_products()
|
||||
}
|
||||
|
||||
// Get available products only
|
||||
fn get_available_products() {
|
||||
let all_products = create_products();
|
||||
let available = [];
|
||||
|
||||
for product in all_products {
|
||||
if product.available {
|
||||
available.push(product);
|
||||
}
|
||||
}
|
||||
|
||||
available
|
||||
}
|
||||
|
||||
// Get a specific product by ID
|
||||
fn get_product_by_id(id) {
|
||||
let products = create_products();
|
||||
|
||||
// Convert ID to integer to ensure type compatibility
|
||||
let search_id = id.to_int();
|
||||
|
||||
for product in products {
|
||||
if product.id == search_id {
|
||||
return product;
|
||||
}
|
||||
}
|
||||
|
||||
#{} // Return empty object if not found
|
||||
}
|
||||
|
||||
// Calculate total price of all products
|
||||
fn calculate_total_price() {
|
||||
let products = create_products();
|
||||
let total = 0.0;
|
||||
|
||||
for product in products {
|
||||
total += product.price;
|
||||
}
|
||||
|
||||
total
|
||||
}
|
||||
|
||||
// Format price with currency symbol
|
||||
fn format_price(price) {
|
||||
"$" + price.to_string()
|
||||
}
|
||||
|
||||
// Get all product features as a flattened array
|
||||
fn get_all_features() {
|
||||
let products = create_products();
|
||||
let all_features = [];
|
||||
|
||||
for product in products {
|
||||
for feature in product.features {
|
||||
all_features.push(feature);
|
||||
}
|
||||
}
|
||||
|
||||
all_features
|
||||
}
|
||||
|
||||
// Get a user object
|
||||
fn get_user() {
|
||||
#{
|
||||
name: "John Doe",
|
||||
email: "john@example.com",
|
||||
role: "admin",
|
||||
settings: #{
|
||||
theme: "dark",
|
||||
notifications: true,
|
||||
language: "en"
|
||||
},
|
||||
orders: [
|
||||
#{
|
||||
id: "ORD-001",
|
||||
date: "2025-03-25",
|
||||
total: 1299.99
|
||||
},
|
||||
#{
|
||||
id: "ORD-002",
|
||||
date: "2025-04-01",
|
||||
total: 249.99
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user