ITADN

Rendering text() after comment() on a different line

#214Openmpenning 创建于 2025-05-04
M
mpenningcommented
Thanks for building such a great Python HTML DSL... I have a question about using `dominate.util.text()` after `dominate.util.comment()`. When I render (pretty) the html using this script, my text stays in the same line as the comment. I want the text on a different line... see QUESTION below... I'm using Python 3.12.10 and dominate 2.9.1... ## dominate script ```python import dominate from dominate.util import raw, text from dominate.tags import meta, link, script, comment from dominate.tags import form, label, input_ from dominate.tags import table, thead, tbody, tr, td from dominate.tags import button from dominate.tags import style, div, span, ul, li from dominate.tags import br, a, p, attr from dominate.tags import h1, h2, h3, h4, h5 class DemoDocument(dominate.document): def __init__(self, *args, title="__nothing__", **kwargs): kwargs["title"] = title super().__init__(*args, **kwargs) self.build_doc_structure() self.add_content() def build_doc_structure(self): with self.head: meta(charset="utf-8") link(rel="stylesheet", href="//localhost/css/bootstrap.min.css") script(src="//localhost/js/bootstrap.bundle.min.js") with self: # Add some page structure... div(cls="container", id="header") div(cls="container", id="main-content") div(cls="container", id="footer") def add_content(self, content="hello world"): with self.body.getElementById("main-content"): comment("This comment is on a line by itself") p(content) # QUESTION: How do I get the rendered html comment and text to be on lines by themselves? comment("I want this comment to be on a line by itself") text("Dummy text should be on another line") if __name__=="__main__": doc = DemoDocument() print(doc.render(pretty=True)) ``` ## Rendered HTML This renders as: ```html <!DOCTYPE html> <html> <head> <title>__nothing__</title> <meta charset="utf-8"> <link href="//localhost/css/bootstrap.min.css" rel="stylesheet"> <script src="//localhost/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container" id="header"></div> <div class="container" id="main-content"> <!--This comment is on a line by itself--> <p>hello world</p> <!--I want this comment to be on a line by itself-->Dummy text should be on another line </div> <div class="container" id="footer"></div> </body> </html> ```
0 条评论