Skip to content

Commit 0b9314e

Browse files
committed
Set up Jinja support in demo site
1 parent 1e242a3 commit 0b9314e

File tree

7 files changed

+93
-7
lines changed

7 files changed

+93
-7
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<blockquote class="quote-block block--spacing">
2+
<div class="quote-block__text">
3+
<p class="quote-block__quote heading--3">{{ value.quote }}</p>
4+
{% if value.attribution %}
5+
<p class="quote-block__attribution">{{ value.attribution }}</p>
6+
{% endif %}
7+
</div>
8+
</blockquote>
9+
<p>{{ value.quote|wordcount }} wise words!</p>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
context:
2+
value:
3+
quote: Jinja is the best template engine.
4+
attribution: The Dalai Lama

demo/jinja2.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from django.apps import apps
2+
from django.contrib.staticfiles.storage import staticfiles_storage
3+
from django.template.context_processors import csrf
4+
from django.template.defaultfilters import (
5+
cut,
6+
date,
7+
linebreaks,
8+
pluralize,
9+
slugify,
10+
truncatewords,
11+
urlencode,
12+
wordcount,
13+
)
14+
from django.urls import reverse
15+
16+
from jinja2 import Environment
17+
18+
19+
def environment(**options):
20+
env = Environment(**options)
21+
env.globals.update(
22+
{
23+
"static": staticfiles_storage.url,
24+
"url": reverse,
25+
"csrf": csrf,
26+
}
27+
)
28+
env.filters.update(
29+
{
30+
"cut": cut,
31+
"date": date,
32+
"linebreaks": linebreaks,
33+
"pluralize": pluralize,
34+
"slugify": slugify,
35+
"truncatewords": truncatewords,
36+
"urlencode": urlencode,
37+
"wordcount": wordcount,
38+
}
39+
)
40+
return env

demo/settings.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
44

5-
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
5+
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
66
BASE_DIR = os.path.dirname(PROJECT_DIR)
77

88

@@ -64,6 +64,9 @@
6464
TEMPLATES = [
6565
{
6666
"BACKEND": "django.template.backends.django.DjangoTemplates",
67+
"DIRS": [
68+
"demo/core/templates",
69+
],
6770
"APP_DIRS": True,
6871
"OPTIONS": {
6972
"context_processors": [
@@ -74,7 +77,22 @@
7477
],
7578
"builtins": ["pattern_library.loader_tags"],
7679
},
77-
}
80+
},
81+
{
82+
"BACKEND": "django.template.backends.jinja2.Jinja2",
83+
"DIRS": [
84+
"demo/core/jinja",
85+
],
86+
"APP_DIRS": True,
87+
"OPTIONS": {
88+
"environment": "demo.jinja2.environment",
89+
"extensions": [
90+
"jinja2.ext.do",
91+
"jinja2.ext.i18n",
92+
"jinja2.ext.loopcontrols",
93+
],
94+
},
95+
},
7896
]
7997

8098
WSGI_APPLICATION = "demo.wsgi.application"
@@ -115,11 +133,14 @@
115133
STATICFILES_DIRS = [
116134
# "static_compiled" is a folder used by the front-end tooling
117135
# to output compiled static assets.
118-
os.path.join(PROJECT_DIR, "demo", "static_compiled")
136+
os.path.join(PROJECT_DIR, "static_compiled")
119137
]
120138

121139
PATTERN_LIBRARY = {
122-
"SECTIONS": (("components", ["patterns/components"]),),
140+
"SECTIONS": (
141+
("components", ["patterns/components"]),
142+
("jinja", ["patterns_jinja/jinja_components"]),
143+
),
123144
"PATTERN_BASE_TEMPLATE_NAME": "patterns/base.html",
124145
}
125146

demo/storybook/yaml.stories.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ import quote_block from '../core/templates/patterns/components/streamfield/quote
4141
import quote_blockHTML from '../core/templates/patterns/components/streamfield/quote_block.html';
4242
import quote_blockMD from '../core/templates/patterns/components/streamfield/quote_block.md';
4343

44+
import jinja_quote_block from '../core/jinja/patterns_jinja/jinja_components/quote_block/quote_block.yaml';
45+
import jinja_quote_blockHTML from '../core/jinja/patterns_jinja/jinja_components/quote_block/quote_block.html';
46+
4447
const sources = {
4548
'components/accordion/accordion': [accordion, accordionHTML],
4649
'components/accordion/accordion_section': [
@@ -78,6 +81,10 @@ const sources = {
7881
quote_blockHTML,
7982
quote_blockMD,
8083
],
84+
'jinja_components/quote_block/quote_block': [
85+
jinja_quote_block,
86+
jinja_quote_blockHTML,
87+
],
8188
};
8289

8390
const argTypesify = (obj) => {
@@ -139,7 +146,12 @@ Object.entries(sources).forEach(([path, [rawYAML, source, rawMarkdown]]) => {
139146
const folders = `Django Patterns / ${pathElts
140147
.join('/')
141148
.replace('components/', '')}`;
142-
const htmlPath = `patterns/${path}.html`;
149+
let htmlPath;
150+
if (path.includes('jinja')) {
151+
htmlPath = `patterns_jinja/${path}.html`;
152+
} else {
153+
htmlPath = `patterns/${path}.html`;
154+
}
143155
const description = `${rawMarkdown ?? ''}
144156
### Usage
145157

demo/urls.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
21
from django.urls import include, path
32

4-
urlpatterns = staticfiles_urlpatterns() + [
3+
urlpatterns = [
54
path("pattern-library/", include("pattern_library.urls")),
65
path("", include("demo.storybook.urls")),
76
]

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ django-pattern-library==1.5.0
44
black==22.3.0
55
flake8==4.0.1
66
gunicorn==20.1.0
7+
jinja2==3.1.6

0 commit comments

Comments
 (0)