Render HTML directly into a component in Svelte 3

Tomasz Łakomy
InstructorTomasz Łakomy
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

Disclaimer: please use this feature carefully.

Sometimes we need to render HTML (that comes from a source that we trust!) directly into a component, for instance when we're using a WYSIWYG editor that we control.

In this quick lesson we're going to learn how to use @html in order to render HTML markup from a textarea into a div element.

Instructor: [00:00] We have this simple app with a text area component. Whatever we are going to type in, inside of this text area, we would like to render inside of this diff over here. To do that, we need to bind the value of this text area to this stringToRender variable.

[00:12] In order to do that, bind value = stringToRender. Right now, if I type "Hello Egghead," this text is going to appear over here. I would like to be able to paste in some HTML, and have it rendered inside of this diff. In order to do that, we need to add the @html syntax in front of this string to render variable.

[00:32] If I save, and paste this HTML again, we're going to see it rendered over here. Bear in mind that if you decide to use this feature, Svelte is not going to sanitize any string that you decide to render using @html, so you have to be careful not to expose your users to an attack.

egghead
egghead
~ 6 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today