Tutorial

Render Raw HTML In Your Vue Apps

Published on June 26, 2017
author

Joshua Bemenderfer

Render Raw HTML In Your Vue Apps

Though it’s not that often, sometimes there is actually a good reason for needing to insert raw HTML stored as strings into the DOM. Very rarely, of course. In most cases you should never do this, as this opens you up to a variety of XSS attacks. A somewhat valid use-case might be if you’re writing a new front-end component for an ancient legacy system that (shudders) stores HTML mixed with data in an aging database in the long-forgotten server room in some leased facility upstate. In that case, you might have to resort to rendering raw HTML in your app.

So let’s get started. First things first, break out your RegEx skills

Just kidding. I’d rather not risk the fate of the known universe just to accommodate a legacy system.

Anyway, it turns out that Vue provides us with a nice little directive that can handle all this for us. Predictably, it’s called v-html.

To use it, pass a reference to a HTML string in your data model to v-html in your component template, like so:

<template>
  <div v-html="legacySystemHTML">
  </div>
</template>

<script>
export default {
  data() {
    return {
      // Just like JSX! Oh wait...
      legacySystemHTML: `
        <FONT color="#faddad" size="-2">
          <MARQUEE>Please enter your name to continue.</MARQUEE>
          ...
        </FONT>
      `
    }
  }
}
</script>

And just like that, your HTML will be rendered into the component. If you update the legacySystemHTML property, the HTML will update accordingly.

Not bad!


Also, here’s a little somewhat-related secret. You can use the v-text property the same way, except it only sets the text content of an element.

No idea why you’d use that when you could just use Mustache expressions ({{ componentInnerText }}) instead, but it’s there!

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Joshua Bemenderfer

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.