Skip to content

Commit

Permalink
Update setting name, docs and mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
nanuxbe committed Oct 21, 2024
1 parent 271c4d9 commit 9b942f8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
32 changes: 32 additions & 0 deletions docs/source/integrations/django-bird.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Using the auto settings in `django-bird` will unfortunately create a conflict wi
```python
# settings.py

from dj_angles.mappers.thirdparty import map_bird


DJANGO_BIRD = {
"ENABLE_AUTO_CONFIG": False
} # this is required for `django-bird`
Expand Down Expand Up @@ -47,6 +50,10 @@ TEMPLATES = [
},
},
]

ANGLE = {
"mapper": map_bird
}
```

## Example
Expand All @@ -66,3 +73,28 @@ TEMPLATES = [
{{ slot }}
</button>
```

### Using `default_component_mapper`

```python
# settings.py
ANGLES = {
"default_component_mapper": "dj_angles.mappers.thirdparty.map_bird_component",
}
```

**templates/index.html**

```html
<dj-button class='btn'>
Click me!
</dj-button>
```

**templates/bird/button.html**

```
<button {{ attrs }}>
{{ slot }}
</button>
```
6 changes: 5 additions & 1 deletion docs/source/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ Lower-cases the tag. Useful when using [React-style includes](#react-style-inclu

## `mappers`

Provide additional mappers. `Dictionary` which defaults to `{}`. More details about [mappers](mappers.md).
Provide additional mappers. `Dictionary` which defaults to `{}`. More details about [mappers](mappers.md).

## `default_component_mapper`

Provide a default mapper. This defaults to `dj_angles.mappers.include.map_include`
11 changes: 10 additions & 1 deletion src/dj_angles/mappers/thirdparty.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Callable
import logging
from typing import TYPE_CHECKING

Expand All @@ -11,10 +12,18 @@


def map_bird(tag: "Tag") -> str:
return _stub_map_bird(tag, lambda tag: get_attribute_value_or_first_key(tag, "template"))


def map_bird_component(tag: "Tag") -> str:
return _stub_map_bird(tag, lambda tag: tag.component_name)


def _stub_map_bird(tag: "Tag", get_template_for_tag: Callable[["Tag"], str]) -> str:
if tag.is_end:
return "{% endbird %}"

template = get_attribute_value_or_first_key(tag, "template")
template = get_template_for_tag(tag)
django_template_tag = f"{{% bird {template}"

if tag.attributes:
Expand Down
3 changes: 1 addition & 2 deletions src/dj_angles/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from dj_angles.attributes import Attributes
from dj_angles.mappers.angles import map_angles_include
from dj_angles.mappers.include import map_include
from dj_angles.settings import get_setting

if TYPE_CHECKING:
Expand Down Expand Up @@ -96,7 +95,7 @@ def get_django_template_tag(self, slots: Optional[list[tuple[str, Element]]] = N
if self.django_template_tag is None:
# Assume any missing template tag should use the fallback mapper
self.django_template_tag = import_string(
get_setting("fallback_mapper", "dj_angles.mappers.include.map_include")
get_setting("default_component_mapper", "dj_angles.mappers.include.map_include")
)

# Add component name to the template tags
Expand Down

0 comments on commit 9b942f8

Please sign in to comment.