Component Generator

The Component Generator creates reusable UI components that can be shared across different pages and templates in your Azu application.

Usage

azu generate component COMPONENT_NAME [OPTIONS]

Description

Components in Azu applications are reusable UI elements that encapsulate both presentation logic and styling. They can be used across multiple pages to maintain consistency and reduce code duplication. Components can include forms, cards, navigation elements, and other UI patterns.

Options

  • COMPONENT_NAME - Name of the component to generate (required)

  • -d, --description DESCRIPTION - Description of the component

  • -t, --template TEMPLATE - Template to use (default: basic)

  • -f, --force - Overwrite existing files

  • -h, --help - Show help message

Examples

Generate a basic component

This creates:

  • src/components/user_card.cr - The component class

  • src/components/user_card.jinja - The component template

  • spec/components/user_card_spec.cr - Test file

Generate a component with description

Generate specific component types

Generated Files

Component Class (src/components/COMPONENT_NAME.cr)

Component Template (src/components/COMPONENT_NAME.jinja)

Test File (spec/components/COMPONENT_NAME_spec.cr)

Component Patterns

Basic Component Pattern

Component with Data Processing

Form Component Pattern

Template Patterns

Basic Component Template

List Component Template

Form Component Template

Using Components

In Pages

Use components in your page classes:

In Templates

Include components in your templates:

In Layouts

Use components in your layout templates:

Best Practices

1. Keep Components Focused

Each component should have a single responsibility:

2. Use Flexible Parameters

Make components configurable through parameters:

3. Handle Missing Data Gracefully

4. Use Semantic HTML

Testing Components

Unit Testing

Integration Testing

Common Component Types

1. Display Components

Show data in a consistent format:

2. Form Components

Reusable form elements:

3. Navigation Components

Site navigation elements:

4. Layout Components

Structural layout elements:

  • azu generate page - Generate page components

  • azu generate endpoint - Generate API endpoints

  • azu generate model - Generate data models

  • azu generate service - Generate business logic services

Templates

The component generator supports different templates:

  • basic - Simple component with basic structure

  • card - Card display component template

  • form - Form component template

  • navigation - Navigation component template

  • list - List display component template

To use a specific template:

Last updated