leforestier / yattag

Python library to generate HTML or XML in a readable, concise and pythonic way.
332 stars 31 forks source link

Add declaration #49

Closed kenahoo closed 5 years ago

kenahoo commented 5 years ago

Is there a way to add a declaration at the top of an XML document I'm generating, e.g. <?xml version="1.0" encoding="UTF-8"?>?

jacobluke89 commented 5 years ago

Hi there,

I think this could work.

def build(textBlock):
    """
    builds the html 
    """
    doc, tag, text = Doc().tagtext()
    doc.asis(textBlock)
    return doc.getvalue()

xmlBlock = "<?xml version="1.0" encoding="UTF-8"?>"
build(xmlBlock)

The 'asis' method basically means it will take the variable in as it comes.

Let me know if this works.

leforestier commented 5 years ago

@jacobluke121: while it's true that the asis method should be used here, your example looks like it would always return the same string (just the XML declaration).

@kenahoo: so, yes, use the asis method. There's an example in the tutorial about adding a HTML doctype, and it's the same for XML declarations: https://www.yattag.org/#appending-strings-as-is

kenahoo commented 5 years ago

Okay, thanks. I was hoping for something that would let me avoid remembering the declaration syntax every time I write a document, but this does work.

jacobluke89 commented 5 years ago

@leforestier thanks for pointing this out. This makes sense.