Skip to content

Commit 4dca246

Browse files
William-BlackieWilliam
andauthored
Implement optional default for override_tag (#125)
* Implement optional default for overide_tag * WIP: Update from internal review * Updates tests * Update Docs * Tweak documentation from review Co-authored-by: William <[email protected]>
1 parent db555cf commit 4dca246

File tree

6 files changed

+80
-3
lines changed

6 files changed

+80
-3
lines changed

docs/overview.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ The override process has two parts:
134134
1. Override your template tag with a mock implementation
135135
2. Define fake result for your tag in a `yaml` file
136136

137+
138+
### Providing a default value for template tags
139+
To provide a default for a template tag, you need to provide a keyword argument default_html when overriding your tag.
140+
141+
```
142+
from pattern_library.monkey_utils import override_tag
143+
144+
override_tag(register, 'a_tag_name', default_html="https://potato.com")
145+
```
146+
147+
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.
148+
149+
#### Limitation
150+
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 %}.
137151
### When do I need to override a template tag?
138152
139153
Ideally your pattern library should be independent, so it doesn't fail when

pattern_library/monkey_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
)
88

99
logger = logging.getLogger(__name__)
10+
UNSPECIFIED = object()
1011

1112

12-
def override_tag(register, name):
13+
def override_tag(register, name, default_html=None):
1314
"""
1415
An utility that helps you override original tags for use in your pattern library.
1516
@@ -75,9 +76,13 @@ def node_render(context):
7576

7677
# Render result instead of the tag
7778
return result
79+
elif default_html is not UNSPECIFIED:
80+
# Render provided default;
81+
# if no stub data supplied.
82+
return default_html
7883
else:
7984
logger.warning(
80-
'No stub data defined for the "%s" tag in the "%s" template',
85+
'No default or stub data defined for the "%s" tag in the "%s" template',
8186
tag_name, current_template_name
8287
)
8388

tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@
33
SAND{% error_tag empty_string %}WICH
44
SAND{% error_tag none %}WICH
55
SAND{% error_tag zero %}WICH
6+
7+
POTA{% default_html_tag page.url %}TO
8+
POTA{% default_html_tag page.child.url %}TO
9+
POTA{% default_html_tag None %}TO
10+
11+
POTA{% default_html_tag_falsey empty_string %}TO1
12+
POTA{% default_html_tag_falsey none %}TO2
13+
POTA{% default_html_tag_falsey zero %}TO3

tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,17 @@ tags:
66
raw: null
77
zero:
88
raw: 0
9+
default_html_tag:
10+
page.url:
11+
raw: "example"
12+
page.child.url:
13+
raw: "another_example"
14+
# None handled by default html value.
15+
default_html_tag_falsey:
16+
empty_string:
17+
raw: ''
18+
none:
19+
raw: null
20+
zero:
21+
raw: 0
22+

tests/templatetags/test_tags.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,27 @@
55
register = template.Library()
66

77

8+
# Basic template tag
89
@register.simple_tag
910
def error_tag(arg=None):
1011
"Just raise an exception, never do anything"
1112
raise Exception("error_tag raised an exception")
1213

1314

15+
# Template tag to to test setting a default html value.
16+
@register.simple_tag()
17+
def default_html_tag(arg=None):
18+
"Just raise an exception, never do anything"
19+
raise Exception("default_tag raised an exception")
20+
21+
22+
# Template tag to to test setting a default html value that is falsey.
23+
@register.simple_tag()
24+
def default_html_tag_falsey(arg=None):
25+
"Just raise an exception, never do anything"
26+
raise Exception("default_tag raised an exception")
27+
28+
1429
override_tag(register, 'error_tag')
30+
override_tag(register, 'default_html_tag', default_html="https://potato.com")
31+
override_tag(register, 'default_html_tag_falsey', default_html=None)

tests/tests/test_tags.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .utils import reverse
44

55

6-
class TagsTesCase(SimpleTestCase):
6+
class TagsTestCase(SimpleTestCase):
77
def test_falsey_raw_values_for_tag_output(self):
88
response = self.client.get(reverse(
99
'pattern_library:render_pattern',
@@ -12,3 +12,22 @@ def test_falsey_raw_values_for_tag_output(self):
1212
self.assertContains(response, "SANDWICH")
1313
self.assertContains(response, "SANDNoneWICH")
1414
self.assertContains(response, "SAND0WICH")
15+
16+
def test_default_html_override(self):
17+
response = self.client.get(reverse(
18+
'pattern_library:render_pattern',
19+
kwargs={'pattern_template_name': 'patterns/atoms/tags_test_atom/tags_test_atom.html'},
20+
))
21+
22+
self.assertContains(response, "POTAexampleTO")
23+
self.assertContains(response, "POTAanother_exampleTO")
24+
self.assertContains(response, "POTAhttps://potato.comTO")
25+
26+
def test_falsey_default_html_overide(self):
27+
response = self.client.get(reverse(
28+
'pattern_library:render_pattern',
29+
kwargs={'pattern_template_name': 'patterns/atoms/tags_test_atom/tags_test_atom.html'},
30+
))
31+
self.assertContains(response, "POTATO1")
32+
self.assertContains(response, "POTANoneTO2")
33+
self.assertContains(response, "POTA0TO3")

0 commit comments

Comments
 (0)