lustre-labs / lustre

A Gleam web framework for building HTML templates, single page applications, and real-time server components.
https://hexdocs.pm/lustre
MIT License
950 stars 65 forks source link

Add API to dangerously insert HTML fragments without escaping #21

Closed lpil closed 10 months ago

lpil commented 10 months ago

Hello!

I wish to insert pre-formatted HTML into my Lustre application.

It is rendered on the server, so I cannot do this with web component tricks, etc.

I could insert placeholder UUIDs and replace them, but this is awkward, error prone, and means I have to render to a string rather than a builder and then traverse and reallocate it once per replacement.

The ideal for me would to be able to insert the HTML string and have Lustre turn off escaping for this fragment. I'd prefer a horrible name to make it clear to the programmer that they should not be using unless they know what they are doing.

html.div([], [
  element.dangerously_insert_html_fragment_without_escaping(html_string),
])

Thanks!

hayleigh-dot-dev commented 10 months ago

Thanks for the issue.

In react that is done with a prop: dangerouslySetInnerHttml={{__html: 'html string' }}. If we add this I think we would take the same approach and use an attribute rather than an element: this makes it much simpler to handle on the client because we simply discard any lustre children and do no diffing.

So your example would be re-written as:

html.div([attribute.dangerously_set_inner_html(html_string)], [])