Skip to content

Commit e128f89

Browse files
authored
Allow .yml extension (#169)
1 parent 4dacbd5 commit e128f89

File tree

7 files changed

+33
-4
lines changed

7 files changed

+33
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
- Change Poetry version to be `>=1.1.12,<2` in Docker development setup (prevents `JSONDecodeError` issue under Python 3.10) ([#178](https://github.com/torchbox/django-pattern-library/pull/178)).
1515
- Move demo/test app pattern-library from `/pattern-library/` to `/` ([#178](https://github.com/torchbox/django-pattern-library/pull/178)).
16+
- Allow `.yml` extension for YAML files ([#161](https://github.com/torchbox/django-pattern-library/issues/161)).
1617

1718
### Removed
1819

docs/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ We additionally need to customize a base template, so the standalone component c
168168

169169
### Component data
170170

171-
We can provide context and tags overrides for our new component by creating a `quote_block.yaml` YAML file alongside the HTML, at `patterns/components/quote_block/quote_block.yaml` in our example.
171+
We can provide context and tags overrides for our new component by creating a `quote_block.yaml` YAML file alongside the HTML, at `patterns/components/quote_block/quote_block.yaml` in our example. You can use either `.yaml` or `.yml` as the file extension.
172172

173173
```yaml
174174
context:

docs/reference/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ YAML isn’t everyone’s favorite markup language, but it has the advantage of
66

77
Here is what you need to know:
88

9-
- Always use `.yaml` for patterns configuration.
9+
- Use `.yaml` or `.yml` as the file extension for pattern configuration files. If both are present, the `.yaml` file takes precendence.
1010
- Use Mappings in place of Python Dictionaries.
1111
- Use Sequences in place of Python lists (or iterables like QuerySets).
1212
- The pattern library uses [PyYAML](https://pyyaml.org/wiki/PyYAMLDocumentation) in particular

pattern_library/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,11 @@ def get_pattern_config_str(template_name):
149149
try:
150150
context_file = get_template(context_name)
151151
except TemplateDoesNotExist:
152-
return ''
152+
context_name = context_path + '.yml'
153+
try:
154+
context_file = get_template(context_name)
155+
except TemplateDoesNotExist:
156+
return ''
153157

154158
return context_file.render()
155159

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ atom_var }}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
context:
2+
atom_var: 'atom_var value from test_atom.yml (notice the missing "a" in the extension)'

tests/tests/test_utils.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
from django.conf import settings
44
from django.test import SimpleTestCase, override_settings
55

6-
from pattern_library.utils import get_template_ancestors, get_template_dirs
6+
from pattern_library.utils import (
7+
get_pattern_config_str, get_template_ancestors, get_template_dirs
8+
)
79

810

911
class TestGetTemplateAncestors(SimpleTestCase):
@@ -79,3 +81,22 @@ def test_get_template_dirs_list_dirs(self):
7981
'dpl/pattern_library',
8082
'dpl/tests',
8183
])
84+
85+
86+
class TestGetPatternConfigStr(SimpleTestCase):
87+
def test_not_existing_template(self):
88+
result = get_pattern_config_str("doesnotexist")
89+
90+
self.assertEqual(result, "")
91+
92+
def test_atom_yaml(self):
93+
result = get_pattern_config_str("patterns/atoms/test_atom/test_atom.html")
94+
95+
self.assertNotEqual(result, "")
96+
self.assertIn("atom_var value from test_atom.yaml", result)
97+
98+
def test_atom_yml(self):
99+
result = get_pattern_config_str("patterns/atoms/test_atom_yml/test_atom_yml.html")
100+
101+
self.assertNotEqual(result, "")
102+
self.assertIn("atom_var value from test_atom.yml", result)

0 commit comments

Comments
 (0)