The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a template engine for reports

Creating a template engine for reports involves designing a system that takes structured data (like JSON or a database) and merges it with a template (with placeholders or tags) to generate formatted text (e.g., HTML, Markdown, PDF, or plain text reports).

Below is a basic but flexible template engine in Python. It supports:

  • Placeholder replacement using {{ placeholder }}

  • Conditional blocks

  • Loops over data collections


🔧 Code: Template Engine in Python

python
import re from typing import Any, Dict class ReportTemplateEngine: def __init__(self, template: str): self.template = template def render(self, context: Dict[str, Any]) -> str: rendered = self.template rendered = self._render_loops(rendered, context) rendered = self._render_conditionals(rendered, context) rendered = self._render_variables(rendered, context) return rendered def _render_variables(self, text: str, context: Dict[str, Any]) -> str: pattern = re.compile(r"{{s*(w+)s*}}") def replacer(match): key = match.group(1) return str(context.get(key, f'{{{{ {key} }}}}')) return pattern.sub(replacer, text) def _render_conditionals(self, text: str, context: Dict[str, Any]) -> str: pattern = re.compile(r"{% if (w+) %}(.*?){% endif %}", re.DOTALL) def replacer(match): key, content = match.group(1), match.group(2) return content if context.get(key) else "" return pattern.sub(replacer, text) def _render_loops(self, text: str, context: Dict[str, Any]) -> str: pattern = re.compile(r"{% for (w+) in (w+) %}(.*?){% endfor %}", re.DOTALL) def replacer(match): var_name, list_name, content = match.group(1), match.group(2), match.group(3) items = context.get(list_name, []) result = "" for item in items: result += self._render_variables(content, {**context, var_name: item}) return result return pattern.sub(replacer, text)

📄 Example Template

text
Report: {{ title }} {% if author %} Author: {{ author }} {% endif %} Sections: {% for section in sections %} - {{ section }} {% endfor %}

🧪 Example Usage

python
template = """ Report: {{ title }} {% if author %} Author: {{ author }} {% endif %} Sections: {% for section in sections %} - {{ section }} {% endfor %} """ context = { "title": "Monthly Sales Report", "author": "Jane Doe", "sections": ["Overview", "Regional Breakdown", "Forecast"] } engine = ReportTemplateEngine(template) output = engine.render(context) print(output)

✅ Output

diff
Report: Monthly Sales Report Author: Jane Doe Sections: - Overview - Regional Breakdown - Forecast

🔄 Extendable Features

You can extend this engine with:

  • Filters: {{ date | format_date }}

  • Nested context: {{ user.name }}

  • Custom functions or plugins

  • Output to files (HTML, PDF with tools like weasyprint or pdfkit)

  • Markdown rendering

Let me know if you want a web-based, CLI, or Django-style engine version.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About