Markup DSL
Azu's Markup DSL allows you to build HTML components directly in Crystal code with type safety, component composition, and real-time event handling. It provides a clean, readable syntax for generating HTML without template files.
Overview
The Markup DSL provides:
Type-safe HTML generation with compile-time validation
Component composition for reusable UI elements
Real-time event handling for interactive components
Clean, readable syntax that mirrors HTML structure
Performance optimization with minimal allocations
Basic Usage
Simple HTML Generation
class WelcomeComponent < Azu::Component
def content
div class: "welcome" do
h1 "Welcome to Azu!"
p "Build fast, type-safe web applications with Crystal."
button class: "btn btn-primary", onclick: "showFeatures()" do
text "Learn More"
end
end
end
endComponent with Data
HTML Elements
Basic Elements
Forms
Component Composition
Reusable Components
Using Components
Real-time Components
Live Components with Events
Form Components with Validation
Conditional Rendering
If Statements
Case Statements
Loops and Iteration
Basic Loops
Loops with Index
Conditional Loops
Attributes and Properties
Dynamic Attributes
Boolean Attributes
Event Handling
Client-side Events
Server-side Event Handling
Performance Optimization
Lazy Loading
Caching
Best Practices
1. Component Structure
Keep components focused and single-purpose
Use descriptive component names
Separate presentation logic from business logic
Use composition over inheritance
2. Performance
Minimize DOM updates
Use lazy loading for large lists
Cache expensive operations
Optimize event handlers
3. Maintainability
Use consistent naming conventions
Document complex components
Keep components small and focused
Use type-safe data binding
4. Accessibility
Include proper ARIA attributes
Use semantic HTML elements
Ensure keyboard navigation
Provide alternative text for images
Next Steps
Template Engine - Learn about Jinja2 templates
Hot Reloading - Development workflow
Real-time Components - WebSocket integration
Component Examples - Working examples
Ready to build components? Start with the basic examples above, then explore Real-time Components for interactive features.
Last updated
Was this helpful?
