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> </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 ```lisp
(render (hsx ...)) (render-to-string (hsx ...))
; or
(render (hsx ...) :minify t)
``` ```
## License ## License

View file

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

View file

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

View file

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