1

I need to be able to wrap custom HTML around my Vue template content. As you can see below, it's not simply inserting html content using <div v-html="HtmlWrapperTopHalf"> because that will automatically close any unclosed tags. The tags will always be closed in the bottom half of the dynamic HTML. Basically, I'm looking to insert the Vue template code into my dynamic template HTML.

I also can't just assume what's in the dynamic HTML and build that inside the Vue template. This HTML comes from a RichText editor that my customers create on their own. They just insert a placeholder {{MAIN_CONTENT}} in the middle of their dynamic template and that's where the Vue Template functionality should appear.

Page.vue

<template>
    <div>
        //Top half of dynamic XHTML wrapper goes here

        <p>Inner Content Here</p>
        <ComponentA />
        <ComponentB />
        <!-- etc. --->
        <p>More Inner Content</p>

        //Bottom half of dynamic XHTML wrapper goes here
    </div>
</template>

<script>
    axios.get("getHtmlContent").then((result) =>{
       console.log("top", result.data.HtmlWrapperTopHalf)
       console.log("bottom", result.data.HtmlWrapperBottomHalf)
    });
</script>

console.log

top: <div class="my-dynamic-class" id="my-unique-id"><h1>DYNAMIC TITLE</h1>
bottom: <div id="my-dynamic-footer">custom footer text</div></div>

HTML Output:

<div>
    <div class="my-dynamic-class" id="my-unique-id"><h1>DYNAMIC TITLE</h1>

    <p>Inner Content Here<p>
    <div>Hello from ComponentA!!</div>
    <div>Hello from ComponentB!!</div>
    <!-- etc. --->
    <p>More Inner Content<p>

    <div id="my-dynamic-footer">custom footer text</div></div>
</div>

Hopefully this diagram of the concept will help: enter image description here

4
  • What is purpose to separate starting and ending tag? Commented Nov 3, 2021 at 17:21
  • @Daniel the dynamic html initially isn't separated, it is full XHTML with a placeholder in the middle {{MAIN_CONTENT}} and I split it on that delimiter so I can wrap it around the inner page content. Previously in WebForms.aspx (which we are migrating away from) this was easy because using an <asp:Literal> didn't automatically close the open tags from the top half. v-html closes them, though, which breaks the "wrapping" effect. Commented Nov 3, 2021 at 17:32
  • Hmm, I still don't understand that ;) Why in top/bottom parts are unclosed tags? Commented Nov 3, 2021 at 17:44
  • @Daedalus sorry - just a typo. I fixed. Commented Nov 3, 2021 at 19:17

1 Answer 1

0

Given that other components can be a part of this split up html content, using the render syntax to generate a slot within the content seems to be the only alternative.

   render(createElement) {
      return createElement(
         'div', { 'class': 'hello'},
         this.$slots.default
      )
   },

Have your axios component be built out within the render (see docs), and then use this as a slot to fill in within future components.

E.g. <ClientHtmlInject> <Comp1/><Comp2/> </ClientHtmlInjection>

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

12 Comments

I'm worried this method will close off the top HTML's tags also but I'll give this a shot!
@RichC I updated the answer to have the injection I think you are looking for. Does that work?
I mention why I can't use v-html in my post. I don't think this will work for the same reason. :(
Does the bottom half not close all of the tags within the top half @RichC? If so, this injections of '<p>My injected content</p>' /* i.e. this.customHTML*/ should do it
@RichC I think I have an answer for you. Typing it out now.
|

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.