Rename render to render-to-string

This commit is contained in:
paku 2024-06-01 22:49:15 +09:00
parent 6d894ed9a1
commit 626fa3fe3e
4 changed files with 39 additions and 29 deletions

View file

@ -125,12 +125,9 @@ Which generates:
</div>
```
To output HSX as an HTML string, use the `render` method. By default, pretty-printing is enabled, but you can disable it by enabling the `minify` option.
To output HSX as an HTML string, use the `render-to-string` method.
```lisp
(render (hsx ...))
; or
(render (hsx ...) :minify t)
(render-to-string (hsx ...))
```
## License

View file

@ -16,7 +16,7 @@
#:element-props
#:element-children
#:expand-component
#:render))
#:render-to-string))
(in-package #:hsx/element)
;;;; class definitions
@ -63,9 +63,9 @@
;;;; methods
(defmethod render ((element element) &key minify)
(defmethod render-to-string ((element element) &key pretty)
(with-output-to-string (stream)
(write element :stream stream :pretty (not minify))))
(write element :stream stream :pretty pretty)))
(defmethod print-object ((element tag) stream)
(with-accessors ((type element-type)

View file

@ -6,5 +6,5 @@
#:hsx/hsx)
(:export #:hsx
#:defcomp
#:render))
#:render-to-string))
(in-package :hsx)

View file

@ -5,7 +5,7 @@
#:hsx/builtin)
(:import-from #:mstrings)
(:import-from #:hsx/element
#:render))
#:render-to-string))
(in-package :hsx-test/renderer)
(in-readtable mstrings:mstring-syntax)
@ -14,43 +14,54 @@
(test empty-tag
(is (string= "<div></div>"
(render (div)))))
(render-to-string (div)))))
(test tag-with-props
(is (string= "<div prop1=\"value1\" prop2></div>"
(render (div :prop1 "value1" :prop2 t :prop3 nil)))))
(render-to-string
(div :prop1 "value1" :prop2 t :prop3 nil)))))
(test tag-with-children
(is (string= "<p>foo</p>"
(render (p "foo"))))
(render-to-string (p "foo") :pretty t)))
(is (string= #M"<p>
\ <span>foo</span>
\</p>"
(render (p
(span "foo")))))
(render-to-string
(p
(span "foo"))
:pretty t)))
(is (string= #M"<p>
\ foo
\ <span>bar</span>
\</p>"
(render (p
"foo"
(span "bar"))))))
(render-to-string
(p
"foo"
(span "bar"))
:pretty t))))
(test tag-with-props-and-children
(is (string= "<p prop1=\"value1\" prop2>foo</p>"
(render (p :prop1 "value1" :prop2 t :prop3 nil
"foo"))))
(render-to-string
(p :prop1 "value1" :prop2 t :prop3 nil
"foo")
:pretty t)))
(is (string= #M"<p prop1=\"value1\" prop2>
\ foo
\ <span>bar</span>
\</p>"
(render (p :prop1 "value1" :prop2 t :prop3 nil
"foo"
(span "bar"))))))
(render-to-string
(p :prop1 "value1" :prop2 t :prop3 nil
"foo"
(span "bar"))
:pretty t))))
(test self-closing-tag
(is (string= "<img src=\"/background.png\">"
(render (img :src "/background.png")))))
(render-to-string
(img :src "/background.png")
:pretty t))))
(test fragment
(let ((frg (<>
@ -58,14 +69,16 @@
(li "baz"))))
(is (string= #M"<li>bar</li>
<li>baz</li>"
(render frg)))
(render-to-string frg :pretty t)))
(is (string= #M"<ul>
\ <li>foo</li>
\ <li>bar</li>
\ <li>baz</li>
\ <li>brah</li>
\</ul>"
(render (ul
(li "foo")
frg
(li "brah")))))))
(render-to-string
(ul
(li "foo")
frg
(li "brah"))
:pretty t)))))