1
---
2
name: server-side-rendering
3
description: >
4
Implement server-side rendering with template engines, view layers, and
5
dynamic content generation. Use when building server-rendered applications,
6
implementing MVC architectures, and generating HTML on the server.
7
---
8
9
# Server-Side Rendering
10
11
## Table of Contents
12
13
- [Overview](#overview)
14
- [When to Use](#when-to-use)
15
- [Quick Start](#quick-start)
16
- [Reference Guides](#reference-guides)
17
- [Best Practices](#best-practices)
18
19
## Overview
20
21
Build server-side rendered applications using modern template engines, view layers, and data-driven HTML generation with caching, streaming, and performance optimization across Python, Node.js, and Ruby frameworks.
22
23
## When to Use
24
25
- Building traditional web applications
26
- Rendering HTML on the server
27
- Implementing SEO-friendly applications
28
- Creating real-time updating pages
29
- Building admin dashboards
30
- Implementing email templates
31
32
## Quick Start
33
34
Minimal working example:
35
36
```python
37
# app.py
38
from flask import Flask, render_template, request, jsonify
39
from datetime import datetime
40
41
app = Flask(__name__)
42
43
# Custom Jinja2 filters
44
@app.template_filter('currency')
45
def format_currency(value):
46
return f"${value:.2f}"
47
48
@app.template_filter('date_format')
49
def format_date(date_obj):
50
return date_obj.strftime('%Y-%m-%d %H:%M:%S')
51
52
@app.context_processor
53
def inject_globals():
54
"""Inject global variables into templates"""
55
return {
56
'app_name': 'My App',
57
'current_year': datetime.now().year,
58
'support_email': 'support@example.com'
59
}
60
61
# routes.py
62
// ... (see reference guides for full implementation)
63
```
64
65
## Reference Guides
66
67
Detailed implementations in the `references/` directory:
68
69
| Guide | Contents |
70
|---|---|
71
| [Flask with Jinja2 Templates](references/flask-with-jinja2-templates.md) | Flask with Jinja2 Templates |
72
| [Jinja2 Template Examples](references/jinja2-template-examples.md) | Jinja2 Template Examples |
73
| [Node.js/Express with EJS Templates](references/nodejsexpress-with-ejs-templates.md) | Node.js/Express with EJS Templates |
74
| [EJS Template Examples](references/ejs-template-examples.md) | EJS Template Examples |
75
| [Caching and Performance](references/caching-and-performance.md) | Caching and Performance |
76
| [Django Template Examples](references/django-template-examples.md) | Django Template Examples |
77
| [Django Templates](references/django-templates.md) | Django Templates |
78
79
## Best Practices
80
81
### ✅ DO
82
83
- Use template inheritance for DRY code
84
- Implement caching for frequently rendered pages
85
- Use template filters for formatting
86
- Separate concerns between views and templates
87
- Validate and sanitize all user input
88
- Use context processors for global variables
89
- Implement proper pagination
90
- Use conditional rendering appropriately
91
- Cache expensive queries
92
- Optimize template rendering
93
94
### ❌ DON'T
95
96
- Put business logic in templates
97
- Use unbounded loops in templates
98
- Execute database queries in templates
99
- Trust user input without sanitization
100
- Over-nest template inheritance
101
- Use very long template files
102
- Render sensitive data in templates
103
- Ignore template caching opportunities
104
- Use global variables excessively
105
- Mix multiple concerns in one template
106