Template Engine (python, written by Gemini Code Assist)
envgap__gemini__python-t1-35
Written by a coding agent; not on GitHubWritten 2026-03-02
01 / FAILURE SIGNATURE
As the study recorded it
UndefinedError: format_currency filter not registered with Jinja2 env
Not a benchmark task.
- Its repair changed source code, so it is not an environment task.
02 / ENVIRONMENT RECIPE
- Base commit
Not freshly verified- Manifest
requirements.txt- Reproduce
Awaiting issue-specific recipe- Run under trace
Awaiting a meaningful runtime command
03 / TASK AND FAILURE
gemini/python-t1 #35 · read the task the agent was given
Gemini Code Assist wrote this python project from the task below. It does not run on a clean Ubuntu 22.04 machine as written.
Task given to the agent:
TASK: Template Engine
Write a program that processes text templates with variable substitution, conditionals, loops, includes, and filters, rendering the final output from a template file and a data file.
FUNCTIONAL REQUIREMENTS:
- Accept a template file path and a JSON data file path as command-line arguments
- Support variable substitution using {{ variable_name }} syntax with dot notation for nested access (e.g., {{ user.name }})
- Support conditional blocks: {% if condition %} ... {% elif condition %} ... {% else %} ... {% endif %} with support for comparison operators (==, !=, <, >, <=, >=) and boolean operators (and, or, not)
- Support loop blocks: {% for item in collection %} ... {% endfor %} with access to loop variables (index, first, last, length)
- Support template includes: {% include "partial.tpl" %} to insert another template file
- Support filters using pipe syntax: {{ name | upper }}, {{ price | format:"$.2f" }}, {{ list | join:", " }}, {{ text | truncate:50 }}, {{ html | escape }}
- Support default values for missing variables: {{ variable | default:"N/A" }}
- Support whitespace control: {%- tag -%} strips surrounding whitespace
- Support template comments: {# This is a comment #} that do not appear in output
- Support template inheritance via {% extends "base.tpl" %} and {% block name %} ... {% endblock %} for defining overridable sections
- Print the rendered output to console by default
- Save rendered output to a file via --output flag
- Support batch rendering: apply the same template to multiple data files via --batch flag, generating one output file per data file
- If no input is given, generate a sample template (an HTML email template with header, body, footer, product list, conditional discount, and formatted prices) and a sample JSON data file, then render the template and display the result
- Handle errors: undefined variables (configurable: strict mode raises error, lenient mode outputs empty string via --strict flag), syntax errors in templates with line numbers, circular includes, and missing include files
Create a complete Python project for a clean Ubuntu 22.04 machine with only Python 3.10+ installed. Include:
- Source code
- requirements.txt with all dependencies (direct and transitive) pinned to exact versions
- README.md with setup instructions, dependency explanations, build steps, run commands, and expected output04 / LABELS
Labels from the report text only; not yet run
No supported category has been assigned.
Label rules and the text that matched
[]
05 / FILES
The project as the agent wrote it
3 files, exactly as written, before any repair.
README.md
# Template Engine (Python)
A powerful template engine based on Jinja2, supporting variables, loops, conditionals, and filters.
## Setup Instructions
1. Ensure Python 3.10+ is installed.
2. Create and activate a virtual environment:
```bash
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
```
3. Install dependencies:
```bash
pip install -r requirements.txt
```
## Run Commands
- **Run with defaults (Demonstration)**:
```bash
python template_engine.py
```
- **Render specific template and data**:
```bash
python template_engine.py --template my_template.html --data my_data.json --output rendered.html
```
- **Strict mode (error on undefined variables)**:
```bash
python template_engine.py --template my_template.html --data my_data.json --strict
```
## Features
- **Variable Substitution**: `{{ user.name }}`
- **Conditionals**: `{% if ... %} ... {% endif %}`
- **Loops**: `{% for item in items %} ... {% endfor %}`
- **Filters**: `{{ price | format_currency }}`
- **Template Includes**: `{% include "header.html" %}`
- **Inheritance**: `{% extends "base.html" %}`
requirements.txt
Jinja2==3.1.3 MarkupSafe==2.1.5
template_engine.py
import os
import sys
import json
import argparse
from jinja2 import Environment, FileSystemLoader, select_autoescape, StrictUndefined, Undefined
def render_template(template_path, data_path, output_path=None, strict=False):
# Determine the directory of the template for the loader
template_dir = os.path.dirname(os.path.abspath(template_path))
template_name = os.path.basename(template_path)
# Initialize Jinja2 environment
env = Environment(
loader=FileSystemLoader(template_dir),
autoescape=select_autoescape(['html', 'xml']),
undefined=StrictUndefined if strict else Undefined
)
# Load data
with open(data_path, 'r', encoding='utf-8') as f:
data = json.load(f)
# Render template
template = env.get_template(template_name)
rendered_output = template.render(data)
if output_path:
with open(output_path, 'w', encoding='utf-8') as f:
f.write(rendered_output)
print(f"Rendered output saved to: {output_path}")
else:
print(rendered_output)
def generate_samples():
print("No inputs provided. Generating sample template and data...")
# Sample Template (HTML Email)
template_content = """
<!DOCTYPE html>
<html>
<head>
<title>{{ subject }}</title>
<style>
body { font-family: Arial, sans-serif; }
.header { background-color: #f4f4f4; padding: 10px; }
.product { border-bottom: 1px solid #ddd; padding: 5px 0; }
.discount { color: green; font-weight: bold; }
</style>
</head>
<body>
<div class="header">
<h1>Hello, {{ user.name | default("Valued Customer") }}!</h1>
</div>
<p>Thank you for shopping with us. Here are your items:</p>
<div class="products">
{% for item in items %}
<div class="product">
<strong>{{ item.name }}</strong> - {{ item.price | format_currency }}
{% if item.discount %}
<span class="discount">(Discount applied!)</span>
{% endif %}
</div>
{% endfor %}
</div>
{% if total_price > 100 %}
<p class="discount">You saved a lot today!</p>
{% else %}
<p>Check out our premium collection for more savings.</p>
{% endif %}
<div class="footer">
<p>© {{ year }} Template Engine Demo</p>
</div>
</body>
</html>
"""
with open("sample_template.html", "w", encoding='utf-8') as f:
f.write(template_content)
# Sample Data
data_content = {
"subject": "Your Order Summary",
"user": {"name": "Jane Doe"},
"items": [
{"name": "Wireless Mouse", "price": 25.50, "discount": False},
{"name": "Mechanical Keyboard", "price": 89.99, "discount": True},
{"name": "USB-C Hub", "price": 45.00, "discount": False}
],
"total_price": 160.49,
"year": 2026
}
with open("sample_data.json", "w", encoding='utf-8') as f:
json.dump(data_content, f, indent=4)
return "sample_template.html", "sample_data.json"
def main():
parser = argparse.ArgumentParser(description="Jinja2-based Template Engine")
parser.add_argument("--template", help="Path to the template file")
parser.add_argument("--data", help="Path to the JSON data file")
parser.add_argument("--output", help="Path to save the rendered output")
parser.add_argument("--strict", action="store_true", help="Enable strict mode for undefined variables")
args = parser.parse_args()
# Custom filter for currency formatting
def format_currency(value):
return f"${value:,.2f}"
# If no inputs, generate samples
if not args.template or not args.data:
template_file, data_file = generate_samples()
else:
template_file, data_file = args.template, args.data
render_template(template_file, data_file, args.output, args.strict)
if __name__ == "__main__":
main()