HyperChad Template
Template system and DSL for building HyperChad UI components with macros and utilities.
Overview
The HyperChad Template package provides:
- Container Macro:
container!macro for declarative UI construction - Template DSL: Domain-specific language for UI component definition with CSS-like syntax
- Control Flow:
@if,@else,@for,@while,@match,@letfor dynamic templates - Rendering System: Convert containers to HTML strings
- Extension Traits: Utility methods for container collections (
ContainerVecMethods,ContainerVecExt) - Calculation Functions: CSS
calc(),min(),max(),clamp()with viewport units - Color Functions:
rgb()andrgba()with multiple alpha formats, hex colors, named colors - Action System:
fxDSL for interactive behaviors - No-std Support: Core functionality works without standard library (uses
alloc)
Features
Template Macro
container!Macro: Declarative UI component construction- Nested Elements: Support for nested container hierarchies
- Attribute Syntax: CSS-like attribute specification
- Dynamic Content: Runtime content generation and insertion
Control Flow
- Conditionals:
@if,@else if,@elsefor conditional rendering - If-Let:
@if letfor pattern matching with Option/Result types - Loops:
@forfor iterating over collections - While:
@whilefor condition-based loops - Match:
@matchfor pattern matching expressions - Let:
@letfor local variable bindings within templates
Rendering System
- HTML Generation: Convert containers to HTML strings
- Debug Attributes: Optional debug information in output
- Pretty Printing: Formatted HTML output for debugging
- String Conversion: Multiple string conversion utilities
Extension Traits
- ContainerVecMethods: Methods for Vec collections
- ContainerVecExt: Additional utility methods
- RenderContainer: Trait for renderable types
- Conversion Traits: Type conversion utilities
Calculation System
- CSS calc(): Support for CSS calculation expressions
- CSS Math Functions:
min(),max(),clamp()for responsive sizing - Unit Functions:
vw(),vh(),dvw(),dvh()viewport units,percent()for percentages - Math Operations: Add, subtract, multiply, divide operations
- Responsive Units: Percentage and viewport-relative units
- Raw Percent Values: Direct percentage notation (e.g.,
100%,50%)
Color System
- RGB Functions:
rgb()color creation (3 or 4 arguments) - RGBA Functions:
rgba()as alias for 4-argumentrgb() - Alpha Support: Float (0.0-1.0), integer (0-255), or percentage ("50%")
- Hex Colors: Support for 3, 4, 6, and 8-digit hex colors (#fff, #ffff, #ffffff, #ffffffff)
- Named Colors: Built-in color constants (
Color::BLACK,Color::WHITE)
Installation
Add this to your Cargo.toml:
[]
= { = "../hyperchad/template" }
Usage
Basic Template Creation
use ;
// Create a simple template
let template = container! ;
// Convert to HTML
let html = to_html;
// Output: <div><h1>Hello World</h1><p>Welcome to HyperChad!</p></div>
Template with Attributes
use container;
let template = container! ;
Dynamic Content
use ;
// Custom type that can be rendered
// Use in template
let user = UserCard ;
let template = container! ;
Control Flow
use container;
let show_header = true;
let items = vec!;
let template = container! ;
Calculation Functions
use container;
let template = container! ;
Unit Functions
use container;
let template = container! ;
Color Functions
use container;
let template = container! ;
Container Collection Methods
use ;
let template = container! ;
// Convert to string
let html = template.to_string;
// Pretty print for debugging
let pretty_html = template.display_to_string_pretty?;
// Into string (consuming)
let html = template.into_string;
Border Utilities
use ;
use Color;
let template = container! ;
Action Integration
use container;
let template = container! ;
Container List Wrapper
use ;
// Wrap containers in a list
let containers = container! ;
let list = new;
// Iterate over containers
for container in &list
// Convert back to Vec
let vec: = list.into;
Type Conversions
Boolean Conversion
use ToBool;
let visible = some_condition.to_bool;
Border Conversion
use IntoBorder;
let border = .into_border; // (Color, Number)
Action Effect Conversion
use IntoActionEffect;
use ActionType;
let effect = show_by_id.into_action_effect;
No-std Support
The package is no_std compatible:
extern crate alloc;
use ;
use String;
Feature Flags
The package supports the following feature flags:
default: Enables thelogicfeaturelogic: Enables logic features in actions and transformer (conditional rendering, responsive values)fail-on-warnings: Enables strict compilation mode across all dependencies
Dependencies
- hyperchad_template_macros:
container!macro implementation - hyperchad_transformer: Core container and element types
- hyperchad_actions: Interactive action system
- hyperchad_color: Color handling and conversion
- hyperchad_template_actions_dsl: Actions DSL for
fxsyntax - hyperchad_transformer_models: Model types re-exported from transformer
- itoa: Fast integer to string conversion
- ryu: Fast float to string conversion
Integration
This package is designed for:
- UI Component Libraries: Building reusable UI components
- Template Systems: Server-side template rendering
- Static Site Generation: Pre-rendered HTML generation
- Desktop Applications: Native UI component construction
- Web Applications: Dynamic HTML generation
Performance
- No-std Compatible: Minimal runtime overhead
- Zero-cost Abstractions: Compile-time template processing
- Efficient String Generation: Optimized HTML string creation
- Memory Efficient: Minimal allocations for container structures