# TankStopp Templ Optimization ## Overview This document describes the optimization of TankStopp's template system using `a-h/templ` - a compile-time template system for Go that generates type-safe HTML templates. ## Migration Summary The application has been migrated from traditional HTML templates to `a-h/templ` templates, providing: - **Type Safety**: Templates are compiled to Go code with full type checking - **Performance**: Templates are compiled at build time, eliminating runtime parsing - **Component Reusability**: Modular components that can be composed together - **Developer Experience**: IDE support, auto-completion, and compile-time error checking ## Project Structure The new template organization follows a clean architecture: ``` tankstopp/internal/views/ ├── components/ │ ├── layout.templ # Base layout, navbar, footer, cards, etc. │ ├── forms.templ # Form components, inputs, buttons │ └── icons.templ # Icon components with SVG definitions └── pages/ ├── auth.templ # Authentication pages (login, register) ├── dashboard.templ # Dashboard page with statistics ├── fuelstops.templ # Add/edit fuel stop pages ├── vehicles.templ # Vehicle management pages └── settings.templ # Settings page ``` ## Component Architecture ### Layout Components (`components/layout.templ`) #### Base Layout ```go templ BaseLayout(title string, user *models.User, username string) { // Full HTML document structure with navbar, footer, and content area } ``` #### Navigation Components - `Navbar()` - Main navigation bar - `NavItem()` - Individual navigation items - `UserDropdown()` - User account dropdown - `Footer()` - Application footer #### UI Components - `Card()` - Reusable card component - `Alert()` - Alert messages (success, error, warning, info) - `EmptyState()` - Empty state placeholder - `PageHeader()` - Page header with title and subtitle - `Badge()` - Status badges - `ProgressBar()` - Progress indicators - `Modal()` - Modal dialogs - `Tabs()` - Tab navigation - `Pagination()` - Pagination controls ### Form Components (`components/forms.templ`) #### Input Components - `Input()` - Basic text inputs - `NumberInput()` - Number inputs with validation - `DateInput()` - Date picker inputs - `TextArea()` - Multi-line text areas - `Select()` - Dropdown selects - `PasswordInput()` - Password inputs with visibility toggle #### Specialized Selects - `CurrencySelect()` - Currency dropdown - `VehicleSelect()` - Vehicle dropdown - `FuelTypeSelect()` - Fuel type dropdown #### Form Layout - `Form()` - Form wrapper - `FormGroup()` - Input group with label and hints - `FormRow()` - Form row wrapper - `FormCol()` - Form column wrapper - `FormButtons()` - Form action buttons - `InputGroup()` - Input with prefix/suffix ### Icon Components (`components/icons.templ`) Comprehensive icon system with 40+ icons: - `Icon(name, size)` - Basic icon component - `IconWithClass(name, size, class)` - Icon with custom classes Available icons include: fuel, plus, home, car, settings, location, edit, trash, save, user, lock, etc. ## Page Templates ### Authentication Pages (`pages/auth.templ`) - `LoginPage()` - User login form - `RegisterPage()` - User registration form - `AuthLayout()` - Shared layout for auth pages ### Dashboard (`pages/dashboard.templ`) - `DashboardPage()` - Main dashboard with statistics and fuel stops table - `FuelStopsTable()` - Reusable fuel stops table component - `DashboardScript()` - JavaScript for dashboard functionality ### Fuel Stops (`pages/fuelstops.templ`) - `AddFuelStopPage()` - Add new fuel stop form - `EditFuelStopPage()` - Edit existing fuel stop form - `AddFuelStopScript()` - JavaScript for form functionality including: - Auto-calculation of total costs - Current date/time defaults - Nearby gas station finder using Overpass API - Form validation ### Vehicles (`pages/vehicles.templ`) - `VehiclesPage()` - Vehicle management dashboard - `VehicleCard()` - Individual vehicle card component - `AddVehiclePage()` - Add new vehicle form - `EditVehiclePage()` - Edit vehicle form - `VehicleBrandSelect()` - Vehicle brand dropdown - Helper functions for vehicle statistics ### Settings (`pages/settings.templ`) - `SettingsPage()` - Comprehensive settings page with: - Profile settings - Application preferences - Security settings - Data management (import/export) - Account management - `SettingsScript()` - JavaScript for settings functionality ## JavaScript Integration The templ templates include embedded JavaScript using the `script` template type: ```go script DashboardScript() { function applyFilters() { // JavaScript functionality } } ``` This approach provides: - Type-safe JavaScript embedding - Scoped functionality per page - Compile-time validation of JavaScript references ## Usage Examples ### Basic Page Structure ```go templ MyPage(user *models.User, data MyData) { @components.BaseLayout("My Page", user, user.Username) { @components.PageHeader("Subtitle", "My Page Title")
{{.Description}}
{ item.Description }
} }