Turbo Class¶
The Turbo object is a reference to a specific broadcast. Broadcast names can be provided as strings or can be generated by passing in a Django instance.
Turbo('broadcast_name')
Turbo(django_instance)
Turbo objects are responsible for connecting to a broadcast, and rendering blocks of HTML that can be sent to subscribers. The following methods are available:
-
turbo.Turbo.render(self, template_name: str, context: dict = None) → TurboRender:¶ Returns a TurboRender object from a Django template. This rendered template can then be broadcast to subscribers with the TurboRender actions. (eg: append, update, etc…)
Takes a template name and context identical to Django’s render() method.
- Return type
-
turbo.Turbo.render_from_string(self, rendered_template: str) → TurboRender:¶ Returns a TurboRender object from a string.
- Parameters
rendered_template (str) –
- Return type
-
turbo.Turbo.remove(selector=None, id=None)¶ Send a broadcast message to remove an element from a turbo frame.
TurboRender methods¶
Once a template has been rendered, it should be pushed to the client. The following methods let the client page know how and where to position the new content when it is received.
The typical use is to chain render and TurboRender.<action> commands into one logical, easy-to-read statement.
Turbo('broadcast_name').render(
'broadcast.html', {'content': "New message!"}
).update(selector=".alert_box")
Each of the following methods take either an id or selector parameter (or both) to specify which HTML element will receive the action.
-
turbo.TurboRender.append(id=None, selector=None)¶ Add the rendered template to the end of the specified HTML element.
-
turbo.TurboRender.prepend(id=None, selector=None)¶ Add the rendered template to the beginning of the specified HTML element.
-
turbo.TurboRender.replace(id=None, selector=None)¶ Remove and replace the specified HTML element with the rendered template.
-
turbo.TurboRender.update(id=None, selector=None)¶ Replace the contents inside the specified HTML element with the rendered template.
-
turbo.TurboRender.remove(id=None, selector=None)¶ Remove the given HTML element. The rendered template will not be used. As not template is used to remove divs, this can also be called directly from the
TurboHTML element. Ex:Turbo('broadcast_name').remove(id='div_to_remove')
-
turbo.TurboRender.before(id=None, selector=None)¶ Insert the rendered template before the specified HTML element.
-
turbo.TurboRender.after(id=None, selector=None)¶ Insert the template after the specified HTML element.