You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guides/defining-template-context.md
+3-4
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,6 @@ context:
58
58
59
59
You can define a list or a dict or anything that [`PyYAML`](http://pyyaml.org/wiki/PyYAMLDocumentation) allows you to create in YAML format without creating a custom objects.
60
60
61
-
62
61
## Modifying template contexts with Python
63
62
64
63
While most objects can be faked with YAML, Django has a few common constructs that are difficult to replicate. For example: `Form`and `Paginator` instances. To help with this, django-pattern-library allows you to register any number of 'context modifiers'. Context modifiers are simply Python functions that accept the `context` dictionary generated from the YAML file, and can make additions or updates to it as necessary. For convenience, they also receive the current `HttpRequest` as `request`.
### Registering a context modifier for a specific template
137
136
138
-
By default, context modifiers are applied to all pattern library templates. If you only wish for a context modifier to be applied to a specific pattern, you can use the ``template`` parameter to indicate this. For example:
137
+
By default, context modifiers are applied to all pattern library templates. If you only wish for a context modifier to be applied to a specific pattern, you can use the `template` parameter to indicate this. For example:
### Controlling the order in which context modifiers are applied
168
167
169
-
By default, context modifiers are applied in the order they were registered (which can be difficult to predict accross multiple apps), with generic context modifiers being applied first, followed by template-specific ones. If you need to control the order in which a series of context modifiers are applied, you can use the `order` parameter to do this.
168
+
By default, context modifiers are applied in the order they were registered (which can be difficult to predict across multiple apps), with generic context modifiers being applied first, followed by template-specific ones. If you need to control the order in which a series of context modifiers are applied, you can use the `order` parameter to do this.
170
169
171
-
In the following example, a generic context modifier is registered with an `order` value of `1`, while others recieve the default value of `0`. Because `1` is higher than `0`, the generic context modifier will be applied **after** the others.
170
+
In the following example, a generic context modifier is registered with an `order` value of `1`, while others receive the default value of `0`. Because `1` is higher than `0`, the generic context modifier will be applied **after** the others.
Copy file name to clipboardExpand all lines: docs/reference/api.md
+19-3
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ context:
27
27
tags:
28
28
error_tag:
29
29
include:
30
-
template_name: 'non-patterns/include.html'
30
+
template_name: "non-patterns/include.html"
31
31
```
32
32
33
33
## Templates
@@ -121,20 +121,36 @@ PATTERN_LIBRARY = {
121
121
122
122
### `override_tag`
123
123
124
-
This function tells the pattern library which Django tags to override, and optionally supports providing a default value. See [../guides/overriding-template-tags.md] for more information.
124
+
This function tells the pattern library which Django tags to override, and optionally supports providing a default value. See [Overriding template tags](../guides/overriding-template-tags.md) for more information.
125
125
126
126
```python
127
127
from pattern_library.monkey_utils import override_tag
This decorator makes it possible to override or create additional context data with Django / Python code, rather than being limited to YAML. It has to be called from within a `pattern_contexts` module, which can be at the root of any Django app. See [Modifying template contexts with Python](../guides/defining-template-context.md#modifying-template-contexts-with-python) for more information.
135
+
136
+
```python
137
+
# myproject/core/pattern_contexts.py
138
+
139
+
from pattern_library import register_context_modifier
140
+
from myproject.core.forms import SearchForm, SignupForm
141
+
142
+
@register_context_modifier
143
+
def add_common_forms(context, request):
144
+
if 'search_form' not in context:
145
+
context["search_form"] = SearchForm()
146
+
```
147
+
132
148
## Commands
133
149
134
150
### `render_patterns`
135
151
136
152
Renders all django-pattern-library patterns to HTML files, in a directory
137
-
structure. This can be useful for [automated tests](../guides/automated-tests.md)
153
+
structure. This can be useful for [automated tests](../guides/automated-tests.md).
0 commit comments