Skip to content
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

Add support for Jinja2 #108

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,7 @@ calculate_integrity_of_static("index.js", Algorithm.SHA512) # "sha512-..."
Yes. `django-sri` outputs the static file URL in the same way the builtin `static` template tag does. This means the correct cachebusted URLs are output.

When using a manifest `STATICFILES_STORAGE`, `django-sri` will automatically retrieve the hashed and post-processed file as opposed to the original.

### `jinja2`

Support for `jinja2` templates is provided using the `sri.jinja2.sri` extension, which adds the documented Django template tags as global functions. These functions work identically to the Django template versions.
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ mypy==1.14.1
pytest==8.3.4
pytest-django==4.9.0
pytest-cov==6.0.0
jinja2
16 changes: 16 additions & 0 deletions sri/jinja2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from jinja2 import Environment
from jinja2.ext import Extension

from sri.templatetags.sri import sri_integrity_static, sri_static


class SRIExtension(Extension):
def __init__(self, environment: Environment) -> None:
super().__init__(environment)

environment.globals["sri_static"] = sri_static
environment.globals["sri_integrity_static"] = sri_integrity_static


# Shorthand
sri = SRIExtension
8 changes: 8 additions & 0 deletions tests/jinja2/complex.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html lang="en"></html>
<head>
<title>Complex test page</title>
{{ sri_static("index.js", 'defer', 'async') }}
{{ sri_static("index.woff2", "preload", as="font") }}
</head>
</html>
6 changes: 6 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
"APP_DIRS": True,
"OPTIONS": {},
},
{
"BACKEND": "django.template.backends.jinja2.Jinja2",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {"environment": "tests.utils.create_jinja2_environment"},
},
]

SECRET_KEY = "abcde12345"
Expand Down
4 changes: 2 additions & 2 deletions tests/templates/complex.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<html lang="en"></html>
<head>
<title>Complex test page</title>
{% sri_static "index.js" 'defer' 'async'%}
{% sri_static "index.woff2" preload as="font" %}
{% sri_static "index.js" 'defer' 'async' %}
{% sri_static "index.woff2" "preload" as="font" %}
</head>
</html>
14 changes: 13 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_complex_template():
in rendered
)
assert (
'<link as="font" crossorigin="anonymous" href="/static/index.woff2" integrity="sha256-hWU2c2zzSsvKYN7tGMnt3t3Oj7GwQZB2aLRhCWYbFSE=">'
'<link as="font" crossorigin="anonymous" href="/static/index.woff2" integrity="sha256-hWU2c2zzSsvKYN7tGMnt3t3Oj7GwQZB2aLRhCWYbFSE=" preload>'
in rendered
), rendered

Expand All @@ -56,6 +56,18 @@ def test_algorithms_template():
)


def test_jinja2_template():
rendered = render_to_string("complex.j2")
assert (
'<script crossorigin="anonymous" integrity="sha256-VROI/fAMCWgkTthVtzzvHtPkkxvpysdZbcqLdVMtwOI=" src="/static/index.js" defer async></script>'
in rendered
)
assert (
'<link as="font" crossorigin="anonymous" href="/static/index.woff2" integrity="sha256-hWU2c2zzSsvKYN7tGMnt3t3Oj7GwQZB2aLRhCWYbFSE=" preload>'
in rendered
)


@pytest.mark.parametrize("algorithm", sri.Algorithm)
@pytest.mark.parametrize("file", TEST_FILES)
def test_generic_algorithm(algorithm, file):
Expand Down
7 changes: 7 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from jinja2 import Environment


def create_jinja2_environment(**options):
env = Environment(**options)
env.add_extension("sri.jinja2.sri")
return env
Loading