Framefox implements the Model-View-Controller (MVC) architectural pattern to separate concerns and organize your application code. This pattern divides your application into three interconnected components:
Models: Data structures and business logic (Entities/ORM)
class AbstractController: def __init__(self): """ Initializes a new controller with dependency injection container and template renderer. Child controller can override this method to inject their own dependencies. """ self._container = ServiceContainer() self.template_renderer = self._container.get_by_tag("core.templates.template_renderer")
def render(self, template_path, context=None): """ Renders a template file with optional context data and returns an HTML response. Args: template_path (str): Path to the template file to render context (dict, optional): Data to pass to the template (default: None) Returns: HTMLResponse: The rendered template as HTML response """ template_renderer = self._get_container().get_by_name("TemplateRenderer") self._last_rendered_template = template_path if context is None: context = {} try: content = template_renderer.render(template_path, context) return HTMLResponse(content=content) except Exception: raise
Example:
@Route("/profile", "user.profile", methods=["GET"])async def profile(self): user = self.get_user() return self.render("user/profile.html", { "user": user, "title": "My Profile" })
def json(self, data: dict, status: int = 200): """ Returns data as a JSON response with optional HTTP status code. Args: data (dict): The data to serialize as JSON status (int): HTTP status code for the response (default: 200) Returns: JSONResponse: The data serialized as JSON response """ return JSONResponse(content=data, status_code=status)
def redirect(self, location: str, code: int = 302): """ Redirects the user to a specific URL with an optional HTTP status code. Args: location (str): The URL to redirect to code (int): HTTP status code for the redirect (default: 302) """ return RedirectResponse(url=location, status_code=code)
def flash(self, category: str, message: str): """ Adds a flash message to the session for displaying on the next request. Args: category (str): The category/type of the flash message (e.g., 'success', 'error') message (str): The message content to display """ session = self._get_container().get_by_name("Session") flash_bag = session.get_flash_bag() flash_bag.add(category, message)
Example:
@Route("/save", "user.save", methods=["POST"])async def save(self): # Save user data self.flash("success", "Profile updated successfully!") return self.redirect("/profile")
def generate_url(self, route_name: str, **params): """ Generates a URL for a named route with optional parameters. Args: route_name (str): The name of the route to generate URL for **params: Additional parameters to include in the URL Returns: str: The generated URL """ router = self._get_container().get_by_name("Router") return router.url_path_for(route_name, **params)