Spaces:
Sleeping
Sleeping
| # 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!** π | |