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
This change provides a consistent API to render a htpy object as HTML or
iterate over it.
This commit introduces the iter_chunks() method which is identical with
`__iter__()` but with a better name.
With the introduction of Fragment, this commit makes render_node and
iter_node redundant.
This commit deprecates render_node, iter_node and direct iteration of elements.
More info: #86 (comment)
Copy file name to clipboardExpand all lines: docs/changelog.md
+4
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,9 @@
1
1
# Changelog
2
2
3
+
## NEXT
4
+
- Add the `Renderable` protocol, a consistent API to render an `htpy` object as HTML or to iterate over it. `Element`, `Fragment`, `ContextProvider`, and `ContextConsumer` are all `Renderable`.
5
+
- Deprecate `render_node()` and `iter_node()` and direct iteration over elements. Call `Renderable.__str__()` or `Renderable.iter_chunks()` instead. [Read the Usage docs for more details](usage.md#renderable).
6
+
3
7
## 25.4.0 - 2025-04-10
4
8
- Make Context's repr debug friendly [PR #96](https://github.com/pelme/htpy/pull/96). Thanks to Stein Magnus Jodal ([@jodal](https://github.com/jodal)).
5
9
- Strip whitespace around id and class values in CSS selector. Fixes
Copy file name to clipboardExpand all lines: docs/static-typing.md
+21
Original file line number
Diff line number
Diff line change
@@ -47,6 +47,27 @@ def bootstrap_badge(
47
47
48
48
```
49
49
50
+
## Renderable
51
+
52
+
htpy elements, fragments and context objects provides are "renderable". The `Renderable`type provides a consistent API to render a `htpy`objectasHTML.
53
+
54
+
The `Renderable` protocol defines these methods:
55
+
56
+
-`.__str__()`- render as a HTML string by calling `str()`
57
+
-`.__html__()`- render as a HTML string that is safe to use as markup. This makes it possible to directly embed a `Renderable`objectin [Django/Jinja templates](django.md#using-htpy-as-part-of-an-existing-django-template).
58
+
-`.iter_chunks()`- stream the contents as string "chunks". See [Streaming](streaming.md) for more information.
59
+
60
+
All `Renderable`'s are also `Node`'s and can always be used as a child element. You can use this to write reusable components that can be used as a child node but also be rendered by themselves or embedded into a Django or Jinja template:
61
+
62
+
```pycon
63
+
>>>from htpy import div, h1, Renderable
64
+
>>>def my_component(name: str) -> Renderable:
65
+
...return div[h1[f"Hello {name}!"]]
66
+
>>>print(my_component("Dave"))
67
+
<div><h1>Hello Dave!</h1></div>
68
+
69
+
```
70
+
50
71
## Node
51
72
52
73
`Node`is a type alias forall possible objects that can be used as a child
0 commit comments