0

I've got a variable which has html inside. This is for my wysiwyg editor.

data () {
  return {
     HTMLtext: "<p style="text-align: center;"><b>Hospital</b></p><p> 
       </p><p style="text-align: center;"> <b>Physician</b></p>
       <p></p><p style="text-align: center;"> City <b>New York</b>",
     Place: "London"
   }
 }

Now, I want to change this HTMLtext variable in some places. For example I've got variable called place:

Place: "London"

and I want to replace <b>New York</b> with Place variable into <b>London</b>

What should I do? What is the best approach? Is it generally safe? Is this a good practice?

4
  • Generally, no, it's not safe to make arbitrary edits to HTML, and it isn't clear how you would decide to replace, specifically, <b>New York</b>. Can you give us some more description of how this step fits into what you're trying to do? Commented Feb 22, 2018 at 18:24
  • @RoyJ The first problem is just that: how to replace this part of HTML code. The second, later on, how to sanitize Place variable. I want to make my variables reusable, so my user don't need to type them again. It's not a rocket science, so it should be possible :) Commented Feb 22, 2018 at 18:28
  • I would probably make HTMLtext a computed that returns a template string that substitutes in data items, like ...<p style="text-align: center;"> City <b>${this.Place}</b> Commented Feb 22, 2018 at 18:34
  • how to do that? I didn't know about ${this.Place} Commented Feb 22, 2018 at 18:37

1 Answer 1

4

If you make HTMLtext a computed, it can compose the text based on the variables. Template strings are a nice way to interpolate variables into strings.

Here's an example of what I'm suggesting. I don't know if that will work in your circumstance because I don't know where HTMLtext comes from or how you know that New York is in it to be replaced.

new Vue({
  el: '#app',
  data: {
    place: 'New York'
  },
  computed: {
    htmlText() {
      return `<p style="text-align: center;"><b>Hospital</b></p><p> 
       </p><p style="text-align: center;"> <b>Physician</b></p>
       <p></p><p style="text-align: center;"> City <b>${this.place}</b>`;
    }
  }
});
<script src="//unpkg.com/vue@latest/dist/vue.js"></script>
<div id="app">
  Place: <input v-model="place">
  <div v-html="htmlText"></div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

I'll try it, it seems to be the right way. Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.