swiftops-backend / docs /dev /comms /TEMPLATES_ARCHITECTURE.md
kamau1's picture
chore: migrate to useast organize the docs, delete redundant migrations
c4f7e3e
# SwiftOps API - Templates Architecture
## πŸ—οΈ Architecture Overview
Following FastAPI best practices, we've separated concerns into distinct layers:
```
src/app/
β”œβ”€β”€ templates/ # Jinja2 HTML templates
β”‚ β”œβ”€β”€ base.html # Base template (layout)
β”‚ β”œβ”€β”€ index.html # Landing page
β”‚ └── README.md # Template documentation
β”‚
β”œβ”€β”€ static/ # Static assets
β”‚ β”œβ”€β”€ css/
β”‚ β”‚ └── main.css # Styles (theme-aware)
β”‚ └── js/
β”‚ └── main.js # Client-side JavaScript
β”‚
β”œβ”€β”€ api/v1/
β”‚ β”œβ”€β”€ pages.py # Page routes (HTML)
β”‚ β”œβ”€β”€ auth.py # API routes (JSON)
β”‚ └── ... # Other API routes
β”‚
β”œβ”€β”€ constants/
β”‚ └── app_metadata.py # Centralized app config
β”‚
└── main.py # Application entry point
```
---
## πŸ“‹ Design Principles
### 1. **Separation of Concerns**
- **Templates** (`templates/`) - HTML structure
- **Styles** (`static/css/`) - Visual presentation
- **Scripts** (`static/js/`) - Client behavior
- **Routes** (`api/v1/pages.py`) - Page logic
- **Config** (`constants/app_metadata.py`) - Metadata
### 2. **Template Inheritance**
```
base.html (layout)
↓
index.html (content)
↓
Future pages extend base.html
```
### 3. **Static File Serving**
```python
# Mounted in main.py
app.mount("/static", StaticFiles(directory="static"), name="static")
# Used in templates
<link rel="stylesheet" href="{{ url_for('static', path='/css/main.css') }}">
```
### 4. **Centralized Configuration**
```python
# constants/app_metadata.py
APP_NAME = "SwiftOps API"
APP_VERSION = "1.0.0"
# Used everywhere
from app.constants.app_metadata import APP_NAME, APP_VERSION
```
---
## 🎨 Features
### Theme Support
- βœ… Automatic dark/light mode detection
- βœ… Respects system preferences
- βœ… Smooth transitions
- βœ… CSS custom properties (variables)
### Responsive Design
- βœ… Mobile-first approach
- βœ… Flexible grid layouts
- βœ… Touch-friendly interactions
- βœ… Breakpoints for all devices
### Performance
- βœ… Minimal CSS/JS
- βœ… No external dependencies
- βœ… Fast page loads
- βœ… Optimized assets
---
## πŸ“ Adding New Pages
### Step 1: Create Template
```html
<!-- templates/about.html -->
{% extends "base.html" %}
{% block title %}About - {{ app_name }}{% endblock %}
{% block content %}
<div class="container">
<h1>About {{ app_name }}</h1>
<p>{{ app_description }}</p>
</div>
{% endblock %}
```
### Step 2: Add Route
```python
# api/v1/pages.py
@router.get("/about", response_class=HTMLResponse)
async def about(request: Request):
return templates.TemplateResponse("about.html", {
"request": request,
"app_name": APP_NAME,
"app_description": APP_DESCRIPTION
})
```
### Step 3: Update Navigation (if needed)
```html
<!-- templates/base.html -->
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
</nav>
```
---
## 🎯 Best Practices
### 1. **Keep Templates Simple**
- Logic in Python (routes)
- Presentation in HTML (templates)
- Styling in CSS (separate file)
### 2. **Use Template Inheritance**
```html
{% extends "base.html" %}
{% block content %}...{% endblock %}
```
### 3. **Centralize Configuration**
```python
# Don't hardcode
app_name = "SwiftOps API"
# Do this
from app.constants.app_metadata import APP_NAME
```
### 4. **Include Request Object**
```python
# Always pass request to templates
return templates.TemplateResponse("page.html", {
"request": request, # Required by Jinja2Templates
"data": data
})
```
### 5. **Use url_for for Static Files**
```html
<!-- Good -->
<link href="{{ url_for('static', path='/css/main.css') }}">
<!-- Avoid -->
<link href="/static/css/main.css">
```
---
## πŸ”§ Customization
### Update App Metadata
Edit `constants/app_metadata.py`:
```python
APP_NAME = "Your App Name"
APP_VERSION = "2.0.0"
APP_DESCRIPTION = "Your description"
```
### Add Custom Styles
Edit `static/css/main.css`:
```css
/* Add your custom styles */
.custom-class {
color: var(--accent-color);
}
```
### Add Custom JavaScript
Edit `static/js/main.js`:
```javascript
// Add your custom scripts
document.addEventListener('DOMContentLoaded', function() {
// Your code here
});
```
---
## πŸ“Š File Structure Benefits
### βœ… **Maintainability**
- Clear separation of concerns
- Easy to find and update files
- Consistent structure
### βœ… **Scalability**
- Add new pages easily
- Extend base template
- Reuse components
### βœ… **Testability**
- Routes are separate from templates
- Logic is in Python (testable)
- Templates are pure HTML
### βœ… **Performance**
- Static files cached by browser
- Minimal dependencies
- Fast rendering
---
## πŸš€ Future Enhancements
### Planned Features
- [ ] Component library (reusable blocks)
- [ ] Admin dashboard template
- [ ] Error pages (404, 500)
- [ ] Email templates
- [ ] PDF generation templates
### Possible Additions
- [ ] Template fragments (HTMX support)
- [ ] Internationalization (i18n)
- [ ] Template caching
- [ ] Asset bundling/minification
---
## πŸ“š Resources
- [FastAPI Templates](https://fastapi.tiangolo.com/advanced/templates/)
- [Jinja2 Documentation](https://jinja.palletsprojects.com/)
- [Static Files in FastAPI](https://fastapi.tiangolo.com/tutorial/static-files/)
- [CSS Custom Properties](https://developer.mozilla.org/en-US/docs/Web/CSS/--*)
---
**This architecture provides a solid foundation for building scalable, maintainable web pages in your FastAPI application!** πŸŽ‰