diff --git a/docs/overview.md b/docs/overview.md index b3648dab..617237cf 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -134,6 +134,20 @@ The override process has two parts: 1. Override your template tag with a mock implementation 2. Define fake result for your tag in a `yaml` file + +### Providing a default value for template tags +To provide a default for a template tag, you need to provide a keyword argument default_html when overriding your tag. + +``` +from pattern_library.monkey_utils import override_tag + +override_tag(register, 'a_tag_name', default_html="https://potato.com") +``` + +This default is used for any tag that's not passed its own context, allowing specificity for those elements that need it while preventing the tag from breaking when it's not structural to the component. + +#### Limitation +Currently this feature only supports providing a default for the output of the tag, this does not support modifying context in templates such as {% an_example_tag page.url as example_variable %}. ### When do I need to override a template tag? Ideally your pattern library should be independent, so it doesn't fail when diff --git a/pattern_library/monkey_utils.py b/pattern_library/monkey_utils.py index 3e854215..1b13b26f 100644 --- a/pattern_library/monkey_utils.py +++ b/pattern_library/monkey_utils.py @@ -7,9 +7,10 @@ ) logger = logging.getLogger(__name__) +UNSPECIFIED = object() -def override_tag(register, name): +def override_tag(register, name, default_html=None): """ An utility that helps you override original tags for use in your pattern library. @@ -75,9 +76,13 @@ def node_render(context): # Render result instead of the tag return result + elif default_html is not UNSPECIFIED: + # Render provided default; + # if no stub data supplied. + return default_html else: logger.warning( - 'No stub data defined for the "%s" tag in the "%s" template', + 'No default or stub data defined for the "%s" tag in the "%s" template', tag_name, current_template_name ) diff --git a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html index 09258c7d..c7945bc8 100644 --- a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html +++ b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html @@ -3,3 +3,11 @@ SAND{% error_tag empty_string %}WICH SAND{% error_tag none %}WICH SAND{% error_tag zero %}WICH + +POTA{% default_html_tag page.url %}TO +POTA{% default_html_tag page.child.url %}TO +POTA{% default_html_tag None %}TO + +POTA{% default_html_tag_falsey empty_string %}TO1 +POTA{% default_html_tag_falsey none %}TO2 +POTA{% default_html_tag_falsey zero %}TO3 \ No newline at end of file diff --git a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml index 0fe3edf6..5f65be91 100644 --- a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml +++ b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml @@ -6,3 +6,17 @@ tags: raw: null zero: raw: 0 + default_html_tag: + page.url: + raw: "example" + page.child.url: + raw: "another_example" + # None handled by default html value. + default_html_tag_falsey: + empty_string: + raw: '' + none: + raw: null + zero: + raw: 0 + \ No newline at end of file diff --git a/tests/templatetags/test_tags.py b/tests/templatetags/test_tags.py index 581edb9d..9fedee03 100644 --- a/tests/templatetags/test_tags.py +++ b/tests/templatetags/test_tags.py @@ -5,10 +5,27 @@ register = template.Library() +# Basic template tag @register.simple_tag def error_tag(arg=None): "Just raise an exception, never do anything" raise Exception("error_tag raised an exception") +# Template tag to to test setting a default html value. +@register.simple_tag() +def default_html_tag(arg=None): + "Just raise an exception, never do anything" + raise Exception("default_tag raised an exception") + + +# Template tag to to test setting a default html value that is falsey. +@register.simple_tag() +def default_html_tag_falsey(arg=None): + "Just raise an exception, never do anything" + raise Exception("default_tag raised an exception") + + override_tag(register, 'error_tag') +override_tag(register, 'default_html_tag', default_html="https://potato.com") +override_tag(register, 'default_html_tag_falsey', default_html=None) diff --git a/tests/tests/test_tags.py b/tests/tests/test_tags.py index 8b0904a0..78ba1193 100644 --- a/tests/tests/test_tags.py +++ b/tests/tests/test_tags.py @@ -3,7 +3,7 @@ from .utils import reverse -class TagsTesCase(SimpleTestCase): +class TagsTestCase(SimpleTestCase): def test_falsey_raw_values_for_tag_output(self): response = self.client.get(reverse( 'pattern_library:render_pattern', @@ -12,3 +12,22 @@ def test_falsey_raw_values_for_tag_output(self): self.assertContains(response, "SANDWICH") self.assertContains(response, "SANDNoneWICH") self.assertContains(response, "SAND0WICH") + + def test_default_html_override(self): + response = self.client.get(reverse( + 'pattern_library:render_pattern', + kwargs={'pattern_template_name': 'patterns/atoms/tags_test_atom/tags_test_atom.html'}, + )) + + self.assertContains(response, "POTAexampleTO") + self.assertContains(response, "POTAanother_exampleTO") + self.assertContains(response, "POTAhttps://potato.comTO") + + def test_falsey_default_html_overide(self): + response = self.client.get(reverse( + 'pattern_library:render_pattern', + kwargs={'pattern_template_name': 'patterns/atoms/tags_test_atom/tags_test_atom.html'}, + )) + self.assertContains(response, "POTATO1") + self.assertContains(response, "POTANoneTO2") + self.assertContains(response, "POTA0TO3")