Skip to content

Jinja support #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 50 commits into
base: jinja
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
a6d58a1
Fix pattern name URL regex to account for Windows paths with backslas…
reedjones Aug 18, 2023
c9aaf57
Use the correct iframe width with resize buttons. Fix #226 (#225)
jkevingutierrez Aug 18, 2023
a330dd6
Make it possible to navigate the menu with the keyboard. Fix #202 (#207)
chris-lawton Aug 19, 2023
3aa1e38
Add Django 4.2 to the testing matrix (#220)
simkimsia Aug 19, 2023
e60e8ca
Update CHANGELOG
thibaudcolas Aug 19, 2023
115f88a
Upgrade Gr1n/setup-poetry
thibaudcolas Aug 19, 2023
4d79133
Update GitHub Actions
thibaudcolas Aug 19, 2023
bf91533
Comment poetry.lock for caching support in CI
thibaudcolas Aug 19, 2023
5c97dad
Use the officially recommended way to install Poetry in CI
thibaudcolas Aug 19, 2023
04a5ab1
Re-add explicit deploy job permissions
thibaudcolas Aug 19, 2023
c784586
Fix pipx install spec
thibaudcolas Aug 19, 2023
18c8b04
Switch back to separate tox install
thibaudcolas Aug 19, 2023
a67ae22
Skip CodeQL job when unrelated files are changed
thibaudcolas Aug 19, 2023
33bb401
Release v1.0.1
thibaudcolas Aug 19, 2023
035b1c3
Use poetry-core build backend (#232)
mweinelt Sep 29, 2023
09ef4d4
Update CHANGELOG
Sep 29, 2023
34f3072
Make sure /app is always in sys.path (#234)
Oct 23, 2023
a294ccd
Fix pattern matching for templates using dashes (#230)
zerolab Oct 25, 2023
9f57896
Update CHANGELOG
Oct 25, 2023
d16bcc2
Explicitly allow Django 4.2 (#231)
zerolab Oct 25, 2023
76a4690
Update CHANGELOG
Oct 25, 2023
8aa4f26
Bump version to 1.1.0
Oct 25, 2023
a22f346
Fix heading for 1.1.0 tag in CHANGELOG
Oct 25, 2023
0c64a25
Add date for 1.1.0 release
Oct 25, 2023
052b2ad
Update related-projects.md
thibaudcolas Nov 11, 2023
189f3c9
Update pyproject with Django and Poetry newer versions
maribedran Dec 15, 2023
626438a
Fix error accessing pattern template name
maribedran Dec 15, 2023
f6896c5
Wrap rendered pattern HTML with mark_safe to render correctly on temp…
maribedran Dec 15, 2023
351ca10
Merge remote-tracking branch 'torchbox/main' into jinja
maribedran Dec 15, 2023
1612e05
Fix typos discovered by codespell (#238)
cclauss Jan 9, 2024
dfc31df
Upgrade GitHub Actions versions (#237)
cclauss Jan 9, 2024
e41b502
fix: make it work for django5.0 (updated) (#241)
alxbridge Jan 16, 2024
1c46bc7
Improve handling of non-string values for 'override_tag's 'default_ht…
alxbridge Jan 16, 2024
5efd3a0
Updates for version 1.2.0
alxbridge Jan 16, 2024
829f7ac
Add Python 3.12 to the test matrix, drop Django 4.1 (#242)
cclauss Jan 17, 2024
6810527
Merge branch 'main' of https://github.com/torchbox/django-pattern-lib…
CuriousLearner Feb 24, 2024
6a73acb
Update poetry lock file
CuriousLearner Feb 26, 2024
10b90af
fix: linting
CuriousLearner Feb 26, 2024
2592c71
Run isort
CuriousLearner Feb 26, 2024
13e6632
Merge pull request #1 from lincolnloop/upgrade-jinja-django-5
mtrythall Feb 26, 2024
33a322c
Make support for Django 5.1 official (#251)
mark-meyer Oct 18, 2024
8d4aa61
add django-lookbook (#250)
michael-yin Dec 11, 2024
e41537b
Update usage-tips.md, note md extension for docs (#248)
bmihelac Dec 11, 2024
688568f
Release v1.3.0
thibaudcolas Dec 11, 2024
56a5848
Test with latest Python
thibaudcolas Apr 2, 2025
49612d6
Remove unneeded tests against Django main
thibaudcolas Apr 2, 2025
e2bee8a
Add Django 5.2rc1 to testing matrix
thibaudcolas Apr 2, 2025
556f47a
Try a different way to allow pre-releases in tox
thibaudcolas Apr 2, 2025
a19e547
Merge main + resolve conflicts
CuriousLearner Apr 4, 2025
03fa257
chore: Remove obsolete version from docker-compose
CuriousLearner Apr 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 39 additions & 51 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,41 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- uses: actions/checkout@v4
- run: pipx install "poetry>=1.1.12,<2"
- uses: actions/setup-python@v5
with:
python-version: "3.10"
- uses: Gr1n/setup-poetry@v7
with:
poetry-version: '1.1.12'
python-version: "3.13"
cache: "poetry"
- run: pip install tox
- run: tox -e lint,py310-dj40
- run: tox -e lint,py313-dj51
test_compatibility:
needs: test
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
# Test with all supported Django versions, for all compatible Python versions.
# See https://docs.djangoproject.com/en/4.0/faq/install/#what-python-version-can-i-use-with-django for the official matrix.
# See https://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django for the official matrix.
# Additionally test on Django’s main branch with the most recent Python version.
- python: "3.7"
toxenv: py37-dj32
- python: "3.8"
toxenv: py38-dj32,py38-dj40,py38-dj41
- python: "3.9"
toxenv: py39-dj32,py39-dj40,py39-dj41
toxenv: py39-dj42
- python: "3.10"
# Skip testing Django 4.0, already tested in previous workflow job.
toxenv: py310-dj32,py310-dj41,py310-djmain
# Tentative support for next Python pre-release. For the correct specifier,
# Check: https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json.
- python: "3.11.0-beta.2"
toxenv: py311-dj41
toxenv: py310-dj42,py310-dj51,py310-dj52
- python: "3.11"
toxenv: py311-dj42,py311-dj51,py311-dj52
- python: "3.12"
toxenv: py312-dj42,py312-dj52
- python: "3.13"
toxenv: py313-dj42,py313-dj52,py312-djmain
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- uses: actions/checkout@v4
- run: pipx install "poetry>=1.1.12,<2"
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
- uses: Gr1n/setup-poetry@v7
with:
poetry-version: '1.1.12'
allow-prereleases: true
- run: pip install tox
- run: tox -q
env:
Expand All @@ -55,31 +51,24 @@ jobs:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
node-version-file: ".nvmrc"
- id: node-cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
# Cache node_modules rather than the npm cache, as we rarely update npm packages.
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/.nvmrc') }}-${{ hashFiles('**/package-lock.json') }}
- if: steps.node-cache.outputs.cache-hit != 'true'
run: npm ci --no-audit
- run: npm run build
- uses: actions/setup-python@v3
with:
python-version: "3.10"
- uses: Gr1n/setup-poetry@v7
with:
poetry-version: '1.1.12'
- uses: actions/cache@v3
- run: pipx install "poetry>=1.1.12,<2"
- uses: actions/setup-python@v5
with:
path: ~/.cache/pip
key: ${{ runner.os }}-python-py310-${{ hashFiles('**/pyproject.toml') }}
restore-keys: |
${{ runner.os }}-python-py310-
- run: poetry config virtualenvs.create false
python-version-file: pyproject.toml
cache: "poetry"
- run: poetry install
- run: poetry run django-admin runserver --settings=tests.settings.production --pythonpath=. &
# Docs website build.
Expand All @@ -95,20 +84,19 @@ jobs:
- run: cat pyproject.toml | awk '{sub(/^version = .+/,"version = \"0.0.0.dev\"")}1' > pyproject.toml.tmp && mv pyproject.toml.tmp pyproject.toml
- run: poetry build
- run: mv dist site
- uses: actions/upload-artifact@v3
- uses: actions/configure-pages@v4
- uses: actions/upload-pages-artifact@v3
with:
name: site
path: site
retention-days: 1
deploy_site:
runs-on: ubuntu-latest
needs: build_site
runs-on: ubuntu-latest
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
- uses: JamesIves/[email protected]
with:
branch: gh-pages
folder: site
clean: true
- uses: actions/deploy-pages@v4
10 changes: 10 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ name: "CodeQL"
on:
push:
branches: [main]
paths-ignore:
- '**/*.md'
- '**/*.yml'
- '**/*.html'
- '**/*.scss'
pull_request:
# The branches below must be a subset of the branches above
branches: [main]
paths-ignore:
- '**/*.md'
- '**/*.yml'
- '**/*.html'
- '**/*.scss'
schedule:
- cron: "28 20 * * 5"

Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ var/
*.egg-info/
.installed.cfg
*.egg
poetry.lock
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
Expand Down
58 changes: 58 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,65 @@

## [Unreleased]

## [1.3.0](https://github.com/torchbox/django-pattern-library/releases/tag/v1.3.0) - 2024-12-11

### Added

- Add support for Django 5.1 ([#251](https://github.com/torchbox/django-pattern-library/pull/251))

### Removed

- Drop support for Python 3.8 ([#251](https://github.com/torchbox/django-pattern-library/pull/251))
- Drop support for Django 4.1 ([#242](https://github.com/torchbox/django-pattern-library/pull/242))

### Documentation

- Note requirement for `.md` extension for pattern documentation files ([#248](https://github.com/torchbox/django-pattern-library/pull/248))
- Mention complementary package django-viewcomponent, and django-lookbook as an alternative. ([#250](https://github.com/torchbox/django-pattern-library/pull/250))

### Maintenance

- Test with Python 3.12 ([#242](https://github.com/torchbox/django-pattern-library/pull/242))

## [1.2.0](https://github.com/torchbox/django-pattern-library/releases/tag/v1.2.0) - 2024-01-16

### Added

- Add support for Django 5.0 ([#241](https://github.com/torchbox/django-pattern-library/pull/241))

### Changed

- From Django >= 4.0, calls to `Node.render()` must always return a string, but this app previously allowed non-string values to be passed in the `default_html` parameter to `override_tag`. Passing a non-string now raises a `TypeError` when using Django >= 4.0, and raises a warning for older versions ([issue #211](https://github.com/torchbox/django-pattern-library/issues/211)).

## [1.1.0](https://github.com/torchbox/django-pattern-library/releases/tag/v1.1.0) - 2023-10-25

### Added

- Add support for Django 4.2 ([#231](https://github.com/torchbox/django-pattern-library/pull/231))

### Changed

- Switch to the `poetry-core` build backend ([#232](https://github.com/torchbox/django-pattern-library/pull/232))

### Removed

- Drop support for Python 3.7 ([#231](https://github.com/torchbox/django-pattern-library/pull/231))
- Drop support for Django 4.0 ([#231](https://github.com/torchbox/django-pattern-library/pull/231))

### Fixed

- Ensure the project root is on `sys.path` so tests etc. can be run in by Docker Compose ([#233](https://github.com/torchbox/django-pattern-library/issues/233), [#234](https://github.com/torchbox/django-pattern-library/pull/234))
- Fix URL pattern matching for template with dashes in the file name ([#229](https://github.com/torchbox/django-pattern-library/issues/229), [#230](https://github.com/torchbox/django-pattern-library/pull/230))

## [1.0.1](https://github.com/torchbox/django-pattern-library/releases/tag/v1.0.1) - 2023-08-19

### Fixed

- Disable pointer events on menu chevron to allow clicks ([#202](https://github.com/torchbox/django-pattern-library/issues/202), [#205](https://github.com/torchbox/django-pattern-library/pull/205))
- Improve menu accessibility by using buttons for menu items ([#202](https://github.com/torchbox/django-pattern-library/issues/202), [#207](https://github.com/torchbox/django-pattern-library/pull/207)).
- Fix pattern name URL regex to account for Windows paths with backslash ([#222](https://github.com/torchbox/django-pattern-library/issues/222), [#223](https://github.com/torchbox/django-pattern-library/pull/223))
- Use the correct iframe width with resize buttons ([#226](https://github.com/torchbox/django-pattern-library/issues/226), [#225](https://github.com/torchbox/django-pattern-library/pull/225)).
- Update the project’s test matrix for upcoming Django 4.2 support ([#212](https://github.com/torchbox/django-pattern-library/issues/212),[#220](https://github.com/torchbox/django-pattern-library/pull/220)).

## [1.0.0](https://github.com/torchbox/django-pattern-library/releases/tag/v1.0.0) - 2022-06-10

Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RUN useradd --create-home dpl && \
chown -R dpl:dpl /venv/ /app/

ENV PATH=/venv/bin:/home/dpl/.local/bin:$PATH \
PYTHONPATH=/app/ \
VIRTUAL_ENV=/venv/ \
DJANGO_SETTINGS_MODULE=tests.settings.dev

Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.2'

services:
web:
build: .
Expand Down
4 changes: 3 additions & 1 deletion docs/community/related-projects.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ Here are other projects that are related to django-pattern-library, and may be r
- [django-components](https://github.com/EmilStenstrom/django-components/) – Reusable UI components for Django, going further than template partials.
- [django-component-tags](https://github.com/syse-i/django-component-tags) – Create advanced HTML components using Django Tags.
- [slippers](https://github.com/mixxorz/slippers) – Reusable components for Django, without writing a single line of Python.
- (Jinja only, incompatible but interesting) [Template Components (tcom)](https://tcom.scaletti.dev/) – Write server-side components as single Jinja template files. Use them as HTML tags without doing any importing.
- (Jinja only, incompatible but interesting) [JinjaX](https://jinjax.scaletti.dev/) – Write server-side components as single Jinja template files. Use them as HTML tags without doing any importing.
- [django-viewcomponent](https://github.com/rails-inspire-django/django-viewcomponent) - Build reusable components in Django, inspired by Rails ViewComponent, the components built by django-viewcomponent can be used in both Django template or Python code.

## Alternatives

- [Storybook](https://storybook.js.org/), and in particular [Storybook for Server](https://github.com/storybookjs/storybook/tree/master/app/server) – Storybook integration with server-rendered UI components.
- [Pattern Lab](http://patternlab.io/) – PHP or Node pattern library, from which this project is heavily inspired.
- [Astrum](http://astrum.nodividestudio.com/) – Similar to Pattern Lab, Node based.
- [rikki-patterns](https://github.com/springload/rikki-patterns) – Experimental Django-friendly pattern library generator, for Jinja2 and Nunjucks templates
- [django-lookbook](https://github.com/rails-inspire-django/django-lookbook) - Empower your Django development with this pluggable app for creating a robust component library. Includes preview system, documentation engine, and parameter editor for building modular UI effortlessly.


## Pattern libraries based on Django
Expand Down
4 changes: 2 additions & 2 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ poetry add --dev django-pattern-library

We support:

- Django 3.2, 4.0, 4.1 (experimental), 4.2 (experimental)
- Python 3.7, 3.8, 3.9, 3.10, 3.11 (experimental)
- Django 4.2, 5.0, 5.1
- Python 3.9, 3.10, 3.11, 3.12
- Django Templates only, no Jinja support
- Modern “evergreen” desktop and mobile browsers

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/automated-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Although pattern libraries often start as tools for manual tests during development, they can also be useful for automated UI testing. There are a few benefits to doing UI tests with a pattern library:

- Test the components in isolation. When tests fail, you will know exactly which component has issues, rather than having to inspect whole pages to understand what might have changed.
- Test the components with mock data. One of the issues with UI tests is to have test data for your UIs to render – you can reuse the pattern library data for this purpose (althoug there are [limitations](../guides/multiple-variants.md)).
- Test the components with mock data. One of the issues with UI tests is to have test data for your UIs to render – you can reuse the pattern library data for this purpose (although there are [limitations](../guides/multiple-variants.md)).

## Setting up automated UI tests

Expand Down
3 changes: 2 additions & 1 deletion docs/guides/usage-tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ You may also consider using the [`render_patterns` command](../reference/api.md#

## Document your patterns

Patterns support defining a custom `name` in YAML, as well as rendering fully-fledged documentation in Markdown. Create a file next to the template to document it:
Patterns support defining a custom `name` in YAML, as well as rendering fully-fledged documentation in Markdown. Create a file next to the template to document it, ensuring the filename has a `.md` extension (e.g., `call_to_action.md`):


```markdown
This template can be used in different places. In streamfield block
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ YAML isn’t everyone’s favorite markup language, but it has the advantage of

Here is what you need to know:

- Use `.yaml` or `.yml` as the file extension for pattern configuration files. If both are present, the `.yaml` file takes precendence.
- Use `.yaml` or `.yml` as the file extension for pattern configuration files. If both are present, the `.yaml` file takes precedence.
- Use Mappings in place of Python Dictionaries.
- Use Sequences in place of Python lists (or iterables like QuerySets).
- The pattern library uses [PyYAML](https://pyyaml.org/wiki/PyYAMLDocumentation) in particular
Expand Down
5 changes: 3 additions & 2 deletions pattern_library/loader_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,10 @@ def do_include(parser, token):
isolated_context=isolated_context,
)


def visit_extends(self, node, frame):
"""This method overrides the jinja extends tag
Is called as part of the compiler CodeGenerator
Is called as part of the compiler CodeGenerator
and adds a line to use the template_new_context as
part of the runtime render to pull in the dpl context
Handles visiting extends
Expand Down Expand Up @@ -196,4 +197,4 @@ def template_new_context(

return new_context(
self.environment, self.name, self.blocks, vars, shared, self.globals, locals
)
)
6 changes: 1 addition & 5 deletions pattern_library/management/commands/render_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
from django.test.client import RequestFactory

from pattern_library import get_base_template_names, get_pattern_base_template_name
from pattern_library.utils import (
get_pattern_context,
render_pattern,
get_renderer,
)
from pattern_library.utils import get_pattern_context, get_renderer, render_pattern


class Command(BaseCommand):
Expand Down
Loading