diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ef69776..d9ce9346 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/getting-started.md b/docs/getting-started.md index 103fb0a3..b22aee91 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -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: diff --git a/docs/reference/api.md b/docs/reference/api.md index a8a4efa2..27d10734 100644 --- a/docs/reference/api.md +++ b/docs/reference/api.md @@ -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 diff --git a/pattern_library/utils.py b/pattern_library/utils.py index b55f602f..19b3f90d 100644 --- a/pattern_library/utils.py +++ b/pattern_library/utils.py @@ -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() diff --git a/tests/templates/patterns/atoms/test_atom_yml/test_atom_yml.html b/tests/templates/patterns/atoms/test_atom_yml/test_atom_yml.html new file mode 100644 index 00000000..9635774f --- /dev/null +++ b/tests/templates/patterns/atoms/test_atom_yml/test_atom_yml.html @@ -0,0 +1 @@ +{{ atom_var }} diff --git a/tests/templates/patterns/atoms/test_atom_yml/test_atom_yml.yml b/tests/templates/patterns/atoms/test_atom_yml/test_atom_yml.yml new file mode 100644 index 00000000..4e492ed5 --- /dev/null +++ b/tests/templates/patterns/atoms/test_atom_yml/test_atom_yml.yml @@ -0,0 +1,2 @@ +context: + atom_var: 'atom_var value from test_atom.yml (notice the missing "a" in the extension)' diff --git a/tests/tests/test_utils.py b/tests/tests/test_utils.py index 615ecc67..b5d805a0 100644 --- a/tests/tests/test_utils.py +++ b/tests/tests/test_utils.py @@ -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): @@ -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)