---
description: This rule explains Jinja template syntax and best practices for Python web applications.
globs: **/*.html
alwaysApply: false
---
# Jinja rules
- Use template inheritance:
```html
{# base.html #}
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}Default Title{% endblock %}</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
```
- Use include for components:
```html
{% include "components/user_card.html" %}
```
- Use macros for reusable functions:
```html
{% macro input(name, value='', type='text') %}
<input type="{{ type }}" name="{{ name }}" value="{{ value }}">
{% endmacro %}
```
- Use control structures:
```html
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}!</p>
{% else %}
<p>Please log in.</p>
{% endif %}
```
- Use filters to format data:
```html
<p>{{ text|truncate(30) }}</p>
<p>{{ date|strftime('%B %d, %Y') }}</p>
```
- Use set for local variables:
```html
{% set navigation = [('Home', '/'), ('About', '/about')] %}
```
- Use whitespace control:
```html
{% for item in items -%}
{{ item }}
{%- endfor %}
```
Jinja templating is a powerful Python templating engine commonly used in web frameworks like Flask and Django. This rule provides essential syntax patterns and best practices for Jinja template development to help you create clean, maintainable, and efficient web templates.
The Jinja rule provides a comprehensive guide to Jinja template syntax and patterns for Python web applications. It covers fundamental concepts including template inheritance, components, macros, control structures, and formatting options. This rule helps standardize template development and promotes best practices within your project.
Template inheritance allows you to build a base "skeleton" template with common elements (header, navigation, footer) that child templates can extend and override specific blocks:
<!-- In a base template, you define blocks that child templates can fill -->
{% block content %}{% endblock %}
<!-- In child templates, you extend and customize -->
{% extends "base.html" %}
{% block content %}
<h1>Page specific content</h1>
{% endblock %}
This approach reduces duplication and creates a consistent structure across multiple pages.
The rule emphasizes several methods for creating reusable template components:
Includes - For embedding template fragments:
{% include "components/user_card.html" %}
Macros - For template functions with parameters:
{% macro input(name, value='', type='text') %}
<input type="{{ type }}" name="{{ name }}" value="{{ value }}">
{% endmacro %}
<!-- Later used as -->
{{ input('username', type='email') }}
Jinja provides powerful ways to handle data:
The Jinja rule (jinja.mdc
) is designed to assist you when working with Jinja templates in your Python web projects.
This rule is configured as an "Auto Attached" rule that automatically activates when you're working with HTML files, thanks to its glob pattern (**/*.html
). When you open or create an HTML file in your project, Cursor will intelligently apply this rule's context to the AI, ensuring you receive Jinja-aware assistance.
This means the AI will:
You can also manually invoke this rule at any time by typing @jinja
in the Cmd-K command palette or AI chat, which is helpful when working with files that might contain Jinja but don't match the HTML extension pattern.
Use Jinja's whitespace control modifiers (-
) to create cleaner HTML output by stripping unnecessary whitespace:
{% for item in items -%}
{{ item }}
{%- endfor %}
Jinja's filters are powerful for formatting and transforming data directly in templates:
<!-- Limit text length -->
{{ long_description|truncate(100) }}
<!-- Format dates -->
{{ created_at|strftime('%B %d, %Y') }}
<!-- Escape content -->
{{ user_input|escape }}
For larger applications:
By following these Jinja best practices, you'll create more maintainable and efficient templates for your Python web applications.