Skip to content

Commit

Permalink
add doc about namespace (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-yin authored Sep 5, 2024
1 parent b3c1044 commit ff0be30
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Topics
slot.md
templates.md
context.md
namespace.md
preview.md
testing.md
articles.md
Expand Down
40 changes: 40 additions & 0 deletions docs/source/namespace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# NameSpace

If your project has many components and want them to be organized in a better way, you can use the namespace feature.

```bash
├── components
│   └── testapp # this is the namespace
│   └── example
│   ├── example.html
│   └── example.py
```

```python
from django_viewcomponent import component


@component.register("testapp.example")
class ExampleComponent(component.Component):

template_name = "testapp/example/example.html"

def __init__(self, **kwargs):
self.name = kwargs.get('name', 'World')
```

You can register the component as `testapp.example`

```html
{% load viewcomponent_tags %}

{% component 'testapp.example' name="MichaelYin"%}
{% endcomponent %}
```

To use the component in Django template, you can use `component 'testapp.example'`

Notes:

1. This can help keep the components organized.
2. If you are developing 3-party Django package (`foo`), you can put all components in the `foo` namespace and use them in the package templates, this would not cause conflicts with other components in the project.
37 changes: 37 additions & 0 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,3 +1017,40 @@ def test_field_component_parameter(self):
<div>test 4</div>
"""
assert_dom_equal(expected, rendered)


class TestNameSpace:
@pytest.fixture(autouse=True)
def register_component(self):
# auto discover components and register
from django_viewcomponent import autodiscover_components

autodiscover_components()

def test_component_namespace(self):
template = Template(
"""
{% load viewcomponent_tags %}
{% component 'testapp.example' %}
{% endcomponent %}
"""
)
rendered = template.render(Context({}))
expected = """
<h1>Hello, World!</h1>
"""
assert_dom_equal(expected, rendered)

def test_component_namespace_with_parameters(self):
template = Template(
"""
{% load viewcomponent_tags %}
{% component 'testapp.example' name="MichaelYin"%}
{% endcomponent %}
"""
)
rendered = template.render(Context({}))
expected = """
<h1>Hello, MichaelYin!</h1>
"""
assert_dom_equal(expected, rendered)
1 change: 1 addition & 0 deletions tests/testapp/components/testapp/example/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Hello, {{ self.name }}!</h1>
9 changes: 9 additions & 0 deletions tests/testapp/components/testapp/example/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django_viewcomponent import component


@component.register("testapp.example")
class ExampleComponent(component.Component):
template_name = "testapp/example/example.html"

def __init__(self, **kwargs):
self.name = kwargs.get("name", "World")

0 comments on commit ff0be30

Please sign in to comment.