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

RichC
  • 7,829
  • 21
  • 85
  • 149
  • What is purpose to separate starting and ending tag? – Daniel Nov 03 '21 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 didn't automatically close the open tags from the top half. v-html closes them, though, which breaks the "wrapping" effect. – RichC Nov 03 '21 at 17:32
  • Hmm, I still don't understand that ;) Why in top/bottom parts are unclosed tags? – Daniel Nov 03 '21 at 17:44
  • @Daedalus sorry - just a typo. I fixed. – RichC Nov 03 '21 at 19:17

1 Answers1

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>

tony19
  • 125,647
  • 18
  • 229
  • 307
T.Woody
  • 1,142
  • 2
  • 11
  • 25
  • I'm worried this method will close off the top HTML's tags also but I'll give this a shot! – RichC Nov 03 '21 at 18:25
  • @RichC I updated the answer to have the injection I think you are looking for. Does that work? – T.Woody Nov 03 '21 at 18:26
  • I mention why I can't use v-html in my post. I don't think this will work for the same reason. :( – RichC Nov 03 '21 at 18:32
  • Does the bottom half not close all of the tags within the top half @RichC? If so, this injections of `'

    My injected content

    ' /* i.e. this.customHTML*/` should do it
    – T.Woody Nov 03 '21 at 18:35
  • this would work if `this.customHtml` was possible. I added more detail to the question. – RichC Nov 03 '21 at 18:38
  • 1
    @RichC I think I have an answer for you. Typing it out now. – T.Woody Nov 03 '21 at 19:04
  • ok - I'll check this out and see if it works – RichC Nov 03 '21 at 20:37
  • Starting to think the only option I have is to convert my HTML wrapper to json data and then parse through it and create HTML controls in a component and then use a slot for the inner content. – RichC Nov 18 '21 at 02:00
  • yeah- "this is the way" – RichC Nov 18 '21 at 15:38
  • @t-woody Ran into a huge roadblock. I can't get the string of XHTML into a format that easily converted into something that I can create elements with javascript. – RichC Nov 20 '21 at 20:42
  • @RichC Do you have an example? This might be a separate question? – T.Woody Nov 22 '21 at 23:43
  • @t-woody yeah since this is a different question, I created a new post here: https://stackoverflow.com/questions/70050421/how-to-take-an-xhtml-string-and-render-it-on-a-webpage-using-javascript – RichC Nov 23 '21 at 18:11