📄 Template Document Builder
This page demonstrates loading an HTML template and modifying it dynamically.
using Astralis;
using Invercargill;
using Invercargill.DataStructures;
using Inversion;
/**
* DocumentBuilderTemplate Example
*
* Demonstrates loading an existing HTML template and modifying elements
* using the DocumentModel classes. Shows how to:
* - Define a MarkupTemplate subclass with embedded HTML
* - Register the template as a singleton with WebApplication
* - Inject and use the template in endpoints
* - Use XPath selectors to find specific elements
* - Modify element content, attributes, and classes
* - Handle form POST to update the document state
*
* This example complements DocumentBuilder.vala by showing template-based
* document manipulation rather than building from scratch.
*
* Usage: document-builder-template [port]
*/
// Application state - a simple counter
class AppState : Object {
public int counter { get; set; }
public int total_changes { get; set; }
public string last_action { get; set; }
public DateTime last_update { get; set; }
public AppState() {
counter = 0;
total_changes = 0;
last_action = "Initialized";
last_update = new DateTime.now_local();
}
public void increment() {
counter++;
total_changes++;
last_action = "Incremented";
last_update = new DateTime.now_local();
}
public void decrement() {
counter--;
total_changes++;
last_action = "Decremented";
last_update = new DateTime.now_local();
}
public void reset() {
counter = 0;
total_changes++;
last_action = "Reset";
last_update = new DateTime.now_local();
}
}
/**
* CSS content for the counter page.
* Served as a FastResource for optimal performance with pre-compression.
*/
private const string COUNTER_CSS = """
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
max-width: 700px;
margin: 0 auto;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
}
.card {
background: white;
border-radius: 12px;
padding: 25px;
margin: 15px 0;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}
h1 {
color: #333;
margin-top: 0;
}
.counter-display {
text-align: center;
padding: 30px;
background: #f8f9fa;
border-radius: 8px;
margin: 20px 0;
}
.counter-value {
font-size: 72px;
font-weight: bold;
color: #667eea;
line-height: 1;
}
.counter-label {
color: #666;
font-size: 14px;
text-transform: uppercase;
letter-spacing: 2px;
}
.button-group {
display: flex;
gap: 10px;
justify-content: center;
margin: 20px 0;
}
button {
padding: 12px 24px;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 16px;
font-weight: 600;
transition: all 0.2s;
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}
.btn-primary {
background: #667eea;
color: white;
}
.btn-primary:hover {
background: #5a6fd6;
}
.btn-danger {
background: #e74c3c;
color: white;
}
.btn-danger:hover {
background: #c0392b;
}
.btn-success {
background: #2ecc71;
color: white;
}
.btn-success:hover {
background: #27ae60;
}
.info-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 15px;
margin: 20px 0;
}
.info-item {
background: #f8f9fa;
padding: 15px;
border-radius: 6px;
text-align: center;
}
.info-label {
font-size: 12px;
color: #666;
text-transform: uppercase;
letter-spacing: 1px;
}
.info-value {
font-size: 18px;
font-weight: 600;
color: #333;
margin-top: 5px;
}
.status-positive {
color: #2ecc71 !important;
}
.status-negative {
color: #e74c3c !important;
}
.status-zero {
color: #666 !important;
}
code {
background: #e8e8e8;
padding: 2px 6px;
border-radius: 4px;
font-size: 14px;
}
pre {
background: #263238;
color: #aed581;
padding: 15px;
border-radius: 6px;
overflow-x: auto;
font-size: 13px;
}
.feature-list {
margin: 0;
padding-left: 20px;
}
.feature-list li {
margin: 8px 0;
color: #555;
}
a {
color: #667eea;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
""";
/**
* CounterTemplate - A cached HTML template for the counter page.
*
* This class extends MarkupTemplate to provide a reusable, cached template.
* The HTML is parsed once and cached; new_instance() returns efficient copies.
* CSS is served separately via CounterStyles FastResource for optimal caching.
*/
class CounterTemplate : MarkupTemplate {
///
This page demonstrates loading an HTML template and modifying it dynamically.
The server loads the HTML template and uses XPath selectors to find and modify elements:
// Load the template
var doc = new MarkupDocument.from_string(HTML_TEMPLATE);
// Find and modify elements using XPath
var counter_el = doc.select_one("//div[@id='counter-value']");
counter_el.text_content = counter.to_string();
// Add/remove classes based on state
if (counter > 0) {
counter_el.add_class("status-positive");
}
MarkupDocument.from_string() - Load HTML from templatedoc.select_one(xpath) - Find single element by XPathdoc.select(xpath) - Find multiple elementselement.text_content - Get/set text contentelement.add_class()/remove_class() - Modify CSS classeselement.set_attribute() - Set element attributesdoc.to_result() - Return as HttpResult