Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-yin committed Nov 14, 2023
1 parent 4102d63 commit c98e8e0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions docs/source/template-tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,18 @@ This can help render `turbo-cable-stream-source` in Django template

`<turbo-cable-stream-source>` is a custom element provided by [turbo-rails](https://github.com/hotwired/turbo-rails/blob/097d8f90cf0c5ed24ac6b1a49cead73d49fa8ab5/app/javascript/turbo/cable_stream_source_element.js), with it, we can send Turbo Stream over the websocket connection and update the page in real time.

```{note}
1. To support Actioncable on the server, please install [django-actioncable](https://github.com/AccordBox/django-actioncable).
2. Please import `<turbo-cable-stream-source>` in your JavaScript code.
To import `turbo-cable-stream-source` element to the frontend, there are two ways:

```html
<script type="module">
import 'https://cdn.jsdelivr.net/npm/@hotwired/[email protected]/+esm'
</script>
```

Or you can [Jump start frontend project bundled by Webpack](https://github.com/AccordBox/python-webpack-boilerplate#jump-start-frontend-project-bundled-by-webpack) and install it via `npm install`

After frontend work is done, to support Actioncable on the server, please install [django-actioncable](https://github.com/AccordBox/django-actioncable).

In `routing.py`, register `TurboStreamCableChannel`

```python
Expand All @@ -111,6 +118,8 @@ cable_channel_register(TurboStreamCableChannel)
In Django template, we can subscribe to stream source like this:

```html
{% load turbo_helper %}

{% turbo_stream_from 'chat' view.kwargs.chat_pk %}
```

Expand All @@ -119,6 +128,8 @@ In Django template, we can subscribe to stream source like this:
Then in Python code, we can send Turbo Stream to the stream source like this

```python
from turbo_response.channel_helper import broadcast_render_to

broadcast_render_to(
["chat", instance.chat_id],
template="message_append.turbo_stream.html",
Expand Down
2 changes: 1 addition & 1 deletion src/turbo_response/templatetags/turbo_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ def __repr__(self):
return "<%s>" % self.__class__.__name__

def render(self, context):
from ..cable_channel import TurboStreamCableChannel
from ..channel_helper import generate_signed_stream_key, stream_name_from
from ..consumer import TurboStreamCableChannel

stream_name_array = [
stream_name.resolve(context) for stream_name in self.stream_name_array
Expand Down

0 comments on commit c98e8e0

Please sign in to comment.