CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-robyn

A Super Fast Async Python Web Framework with a Rust runtime.

Pending
Overview
Eval results
Files

templating.mddocs/

Templating

Template rendering system with support for Jinja2 templates and custom template interfaces for building dynamic web applications.

Capabilities

Template Interface

Abstract base class for implementing custom template engines.

from abc import ABC, abstractmethod

class TemplateInterface(ABC):
    def __init__(self): ...

    @abstractmethod
    def render_template(self, *args, **kwargs) -> Response:
        """
        Render a template with given context variables.
        
        Args:
            *args: Positional arguments for template rendering
            **kwargs: Keyword arguments for template context
            
        Returns:
            Response object with rendered template content
        """

Jinja2 Template Engine

Built-in Jinja2 template implementation for rendering HTML templates.

class JinjaTemplate(TemplateInterface):
    def __init__(
        self, 
        directory: str, 
        encoding: str = "utf-8", 
        followlinks: bool = False
    ):
        """
        Initialize Jinja2 template engine.
        
        Args:
            directory: Directory path containing template files
            encoding: Template file encoding (default: utf-8)
            followlinks: Whether to follow symbolic links
        """
    
    def render_template(self, template_name: str, **kwargs) -> Response:
        """
        Render a Jinja2 template with context variables.
        
        Args:
            template_name: Name of template file to render
            **kwargs: Template context variables
            
        Returns:
            Response object with rendered HTML content
        """

Usage Examples

Basic Template Rendering

from robyn import Robyn
from robyn.templating import JinjaTemplate

app = Robyn(__file__)

# Initialize template engine
templates = JinjaTemplate("./templates")

@app.get("/")
def home(request):
    return templates.render_template(
        "home.html",
        title="Welcome",
        user_name="Alice",
        items=["Item 1", "Item 2", "Item 3"]
    )

@app.get("/profile/<user_id>")
def user_profile(request):
    user_id = request.path_params["user_id"]
    
    # Fetch user data (example)
    user_data = {
        "id": user_id,
        "name": "John Doe",
        "email": "john@example.com",
        "posts": 25
    }
    
    return templates.render_template(
        "profile.html",
        user=user_data,
        page_title=f"Profile - {user_data['name']}"
    )

app.start()

Template Files

Example template files in ./templates/ directory:

base.html:

<!DOCTYPE html>
<html>
<head>
    <title>{{ title | default("My App") }}</title>
    <meta charset="utf-8">
</head>
<body>
    <header>
        <h1>My Web Application</h1>
    </header>
    
    <main>
        {% block content %}{% endblock %}
    </main>
    
    <footer>
        <p>&copy; 2024 My Company</p>
    </footer>
</body>
</html>

home.html:

{% extends "base.html" %}

{% block content %}
<h2>{{ title }}</h2>
<p>Welcome, {{ user_name }}!</p>

{% if items %}
<ul>
    {% for item in items %}
    <li>{{ item }}</li>
    {% endfor %}
</ul>
{% endif %}
{% endblock %}

profile.html:

{% extends "base.html" %}

{% block content %}
<div class="profile">
    <h2>User Profile</h2>
    <div class="user-info">
        <p><strong>Name:</strong> {{ user.name }}</p>
        <p><strong>Email:</strong> {{ user.email }}</p>
        <p><strong>Posts:</strong> {{ user.posts }}</p>
    </div>
</div>
{% endblock %}

Custom Template Engine

from robyn import Robyn, Response, Headers, status_codes
from robyn.templating import TemplateInterface

class CustomTemplateEngine(TemplateInterface):
    def __init__(self, template_dir: str):
        self.template_dir = template_dir
    
    def render_template(self, template_name: str, **kwargs) -> Response:
        # Custom template rendering logic
        template_path = f"{self.template_dir}/{template_name}"
        
        with open(template_path, 'r') as file:
            content = file.read()
        
        # Simple variable substitution (example)
        for key, value in kwargs.items():
            content = content.replace(f"{{{key}}}", str(value))
        
        return Response(
            status_code=status_codes.HTTP_200_OK,
            description=content,
            headers=Headers({"Content-Type": "text/html; charset=utf-8"})
        )

app = Robyn(__file__)
custom_templates = CustomTemplateEngine("./custom_templates")

@app.get("/custom")
def custom_page(request):
    return custom_templates.render_template(
        "page.html",
        title="Custom Template",
        content="This uses a custom template engine"
    )

app.start()

Error Handling

from robyn import Robyn, status_codes
from robyn.templating import JinjaTemplate
from jinja2 import TemplateNotFound

app = Robyn(__file__)
templates = JinjaTemplate("./templates")

@app.get("/safe-render/<template_name>")
def safe_render(request):
    template_name = request.path_params["template_name"]
    
    try:
        return templates.render_template(
            f"{template_name}.html",
            message="Template rendered successfully"
        )
    except TemplateNotFound:
        return Response(
            status_code=status_codes.HTTP_404_NOT_FOUND,
            description="Template not found",
            headers={}
        )
    except Exception as e:
        return Response(
            status_code=status_codes.HTTP_500_INTERNAL_SERVER_ERROR,
            description=f"Template rendering error: {str(e)}",
            headers={}
        )

app.start()

Template Configuration

from robyn import Robyn
from robyn.templating import JinjaTemplate

app = Robyn(__file__)

# Configure template engine with custom settings
templates = JinjaTemplate(
    directory="./templates",
    encoding="utf-8",
    followlinks=False
)

# Templates with conditional rendering
@app.get("/dashboard")
def dashboard(request):
    # Check authentication (example)
    is_authenticated = request.headers.get("Authorization") is not None
    
    if is_authenticated:
        return templates.render_template(
            "dashboard.html",
            user_name="Current User",
            notifications=["New message", "System update"],
            show_admin=True
        )
    else:
        return templates.render_template(
            "login.html",
            error_message=None,
            redirect_url="/dashboard"
        )

app.start()

Install with Tessl CLI

npx tessl i tessl/pypi-robyn

docs

authentication.md

core-app.md

exceptions.md

index.md

mcp.md

openapi.md

request-response.md

status-codes.md

templating.md

websocket.md

tile.json