Template Functions
Framefox provides powerful built-in functions that are available in all your Jinja2 templates. These functions are automatically registered as global template variables, making them easy to use without any imports or configuration.url_for()
Generate URLs from named routes with type-safe parameters.Syntax
Usage
In Controllers
Define named routes using the@Route decorator:
Benefits
- Decoupled URLs: Change route paths without updating templates
- Type-safe: Parameter validation at runtime
- Error handling: Returns
#if route not found (logged as error)
asset()
Generate URLs for static assets with automatic versioning.Syntax
Usage
How Versioning Works
-
File exists: Uses MD5 hash of file modification time
-
File not found: Uses current timestamp
Benefits
- Cache busting: Automatic version parameters force browser refresh
- Performance: Browsers cache assets until files change
- CDN-friendly: Works with content delivery networks
csrf_token()
Generate and return a CSRF token for form protection.Syntax
Usage
How It Works
- Generates a unique token for the session
- Stores the token in the session
- Returns the token value for the form
- Server validates the token on form submission
Security
- Required for POST/PUT/DELETE: Protects against CSRF attacks
- Session-based: Each user session has a unique token
- Automatic validation: Framework validates tokens automatically
current_user
Access the currently authenticated user object.Syntax
Usage
User Properties
Access any property defined on your user entity:get_flash_messages()
Retrieve session-based flash messages for one-time notifications.Syntax
Usage
Setting Flash Messages in Controllers
Message Categories
Common categories include:success- Successful operationserror- Error messageswarning- Warning messagesinfo- Informational messages