Cursor Rules for
Jinja

This rule explains Jinja template syntax and best practices for Python web applications.
Back to rules
Type
Frontend
Language(s)
Python
Compatible with
Stats
20 views
0 copies
0 downloads
jinja.mdc
---
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 %}
```