Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add VDOM to HTML util function #435

Closed
rmorshea opened this issue Jul 17, 2021 · 7 comments · Fixed by #835
Closed

add VDOM to HTML util function #435

rmorshea opened this issue Jul 17, 2021 · 7 comments · Fixed by #835
Labels
flag-good-first-issue A well defined and self-contained task. priority-3-low May be resolved one any timeline.
Milestone

Comments

@rmorshea
Copy link
Collaborator

Sample implementation:

from html import escape as html_escape

def vdom_to_html(vdom: Union[str, VdomDict]) -> str:
    """Convert a VDOM dictionary into an HTML string

    Only the following keys are translated to HTML:

    - ``tagName``
    - ``attributes``
    - ``children`` (must be strings or more VDOM dicts)
    """
    if isinstance(vdom, str):
        return vdom

    try:
        tag = vdom["tagName"]
    except TypeError as error:
        raise TypeError(f"Expected a VDOM dictionary or string, not {vdom}") from error

    if "attributes" in vdom:
        attributes = " " + " ".join(
            f'{k}="{html_escape(v)}"' for k, v in vdom["attributes"].items()
        )
    else:
        attributes = ""

    if "children" in vdom:
        children = "".join(map(vdom_to_html, vdom["children"]))
    else:
        children = ""

    return f"<{tag}{attributes}>{children}</{tag}>"
@rmorshea
Copy link
Collaborator Author

@Archmonger there's the function.

@Archmonger
Copy link
Contributor

Archmonger commented Aug 28, 2021

@rmorshea I believe I was looking for the inverse of this. Would be useful for Django purposes (ex. render via Django template, push template into IDOM).

@rmorshea
Copy link
Collaborator Author

That actually exists 🙂: html_to_vdom

@Archmonger
Copy link
Contributor

Yep, but if I recall the conservation was brought up in reference to it not being documented.

@rmorshea
Copy link
Collaborator Author

The documentation is linked to above. Maybe I'm misunderstanding?

@Archmonger
Copy link
Contributor

Oh that's my bad. I must have missed it when scrolling through the docs.

@rmorshea rmorshea added priority-3-low May be resolved one any timeline. type: feature labels Jan 13, 2022
@rmorshea rmorshea added the flag-good-first-issue A well defined and self-contained task. label Feb 9, 2022
@rmorshea
Copy link
Collaborator Author

@acivitillo, if you want to use IDOM for HTML templating, this might be a good thing to try contributing.

@rmorshea rmorshea added this to the 1.0 milestone Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flag-good-first-issue A well defined and self-contained task. priority-3-low May be resolved one any timeline.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants