This commit is contained in:
kristof 2025-04-03 11:50:02 +02:00
parent 5764950949
commit a9a44f96cf
2 changed files with 260 additions and 0 deletions

View File

@ -0,0 +1,146 @@
// Data objects for Terra template integration
// This script defines complex objects that can be accessed from Terra 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
}
]
}
}

View File

@ -0,0 +1,114 @@
<html>
<head>
<title>Product Catalog</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
.product {
border: 1px solid #ddd;
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
}
.product-available {
background-color: #f0fff0;
}
.product-unavailable {
background-color: #fff0f0;
}
.features {
margin-top: 5px;
}
.price {
font-weight: bold;
color: #2a5885;
}
.total {
margin-top: 20px;
text-align: right;
font-size: 1.2em;
}
.user-info {
background-color: #f5f5f5;
padding: 10px;
margin-top: 20px;
border-radius: 5px;
}
</style>
</head>
<body>
<h1>Product Catalog</h1>
<!-- Call Rhai function to get products and iterate over them -->
{% let products = data_objects:get_products() %}
<h2>All Products</h2>
{% for product in products %}
<div class="product {% if product.available %}product-available{% else %}product-unavailable{% endif %}">
<h3>{{ product.name }}</h3>
<div class="price">{{ data_objects:format_price(product.price) }}</div>
<div class="features">
<strong>Features:</strong>
<ul>
{% for feature in product.features %}
<li>{{ feature }}</li>
{% endfor %}
</ul>
</div>
<div>
{% if product.available %}
<span class="available">In Stock</span>
{% else %}
<span class="unavailable">Out of Stock</span>
{% endif %}
</div>
</div>
{% endfor %}
<!-- Call Rhai function to get only available products -->
{% let available_products = data_objects:get_available_products() %}
<h2>Available Products ({{ available_products.len() }})</h2>
{% for product in available_products %}
<div class="product product-available">
<h3>{{ product.name }}</h3>
<div class="price">{{ data_objects:format_price(product.price) }}</div>
</div>
{% endfor %}
<!-- Display total price using Rhai function -->
<div class="total">
Total Catalog Value: {{ data_objects:format_price(data_objects:calculate_total_price()) }}
</div>
<!-- Display user information from Rhai function -->
{% let user = data_objects:get_user() %}
<div class="user-info">
<h2>User Information</h2>
<p><strong>Name:</strong> {{ user.name }}</p>
<p><strong>Email:</strong> {{ user.email }}</p>
<p><strong>Role:</strong> {{ user.role }}</p>
<p><strong>Theme:</strong> {{ user.settings.theme }}</p>
<h3>Order History</h3>
<ul>
{% for order in user.orders %}
<li>
<strong>{{ order.id }}</strong> ({{ order.date }}) -
{{ data_objects:format_price(order.total) }}
</li>
{% endfor %}
</ul>
</div>
<!-- List all unique features -->
<h2>All Features</h2>
<ul>
{% let features = data_objects:get_all_features() %}
{% for feature in features %}
<li>{{ feature }}</li>
{% endfor %}
</ul>
</body>
</html>