Skip to content

Feature: Allow yml extension #169

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

Merged
merged 9 commits into from
Jan 24, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

- 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)).
- Move demo/test app pattern-library from `/pattern-library/` to `/` ([#178](https://github.com/torchbox/django-pattern-library/pull/178)).
- Allow `.yml` extension for YAML files ([#161](https://github.com/torchbox/django-pattern-library/issues/161)).

### Removed

Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ We additionally need to customize a base template, so the standalone component c

### Component data

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.
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.

```yaml
context:
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:

- Always use `.yaml` for patterns configuration.
- Use `.yaml` or `.yml` as the file extension for pattern configuration files. If both are present, the `.yaml` file takes precendence.
- 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
6 changes: 5 additions & 1 deletion pattern_library/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ def get_pattern_config_str(template_name):
try:
context_file = get_template(context_name)
except TemplateDoesNotExist:
return ''
context_name = context_path + '.yml'
try:
context_file = get_template(context_name)
except TemplateDoesNotExist:
return ''

return context_file.render()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ atom_var }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
context:
atom_var: 'atom_var value from test_atom.yml (notice the missing "a" in the extension)'
23 changes: 22 additions & 1 deletion tests/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from django.conf import settings
from django.test import SimpleTestCase, override_settings

from pattern_library.utils import get_template_ancestors, get_template_dirs
from pattern_library.utils import (
get_pattern_config_str, get_template_ancestors, get_template_dirs
)


class TestGetTemplateAncestors(SimpleTestCase):
Expand Down Expand Up @@ -79,3 +81,22 @@ def test_get_template_dirs_list_dirs(self):
'dpl/pattern_library',
'dpl/tests',
])


class TestGetPatternConfigStr(SimpleTestCase):
def test_not_existing_template(self):
result = get_pattern_config_str("doesnotexist")

self.assertEqual(result, "")

def test_atom_yaml(self):
result = get_pattern_config_str("patterns/atoms/test_atom/test_atom.html")

self.assertNotEqual(result, "")
self.assertIn("atom_var value from test_atom.yaml", result)

def test_atom_yml(self):
result = get_pattern_config_str("patterns/atoms/test_atom_yml/test_atom_yml.html")

self.assertNotEqual(result, "")
self.assertIn("atom_var value from test_atom.yml", result)