So, I'm starting with a variable string of HTML that is a UI template created by my users in a RichText editor (saved to an XML file on disk). It will always be valid XHTML. The XML could be as simple as this:
<div>{{FORM_PLACEHOLDER}}</div>
Or as complex as something like this:
<div id="user-customized-content">
<h1 class="display-1">Customized Header</h1>
<p class="mb-3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
<strong>laboris nisi ut aliquip</strong> ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<div class="alert alert-info">
<p>Laboris nisi ut <em>aliquip</em> ex ea commodo consequat</p>
</div>
<h3 class="display-4">Lorem ipsum dolor:</h3>
<form>{{FORM_PLACEHOLDER}}</form>
<p class="mb-3">
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim
id est laborum.
</p>
</div>
But it will ALWAYS have the {{FORM_PLACEHOLDER}}
somewhere in the string of XML. This will specify where exactly the HTML form should be placed inside the HTML Wrapper code.
On my SPA application (we use Vue.js but I don't think it matters what the framework/library is that is used), I retreive the HTML via an axios call and then I need to write the XHTML to the page AROUND my interactive form (example shown below).
With Vue.js, we use "slots" for this. So, the parent component would have the form in it and its child component (HtmlWrapper
in the example below) would have a slot that would wrap around the form.
<template>
<HtmlWrapper>
Name: <input type="text" name="Name" v-validate="'required'" /><br />
Email: <input type="email" name="Email" /><br />
<button @click="dofunction()">Submit Form</button>
</HtmlWrapper>
</template>
<script>
import HtmlWrapper from "@/components/HtmlWrapper.vue"
</script>
Methods that I've already tried that didn't work:
- Splitting the HTML string on the placeholder and injecting the HTML directly above and below the form fields. This didn't work because the tags at the top will automatically close if using Vue or JS to add them to the DOM.
- Converting the XML string to
XmlDocument
in C# and then serializing it to JSON to pass down to the javascript. This worked great and enabled me to iterate through the json to build the HTML around the<slot></slot>
tag BUT then I realized that multiple HTML tags of the same type would get grouped into arrays which corrupts the order they appear on the page.
What I think that needs to happen is I need to iterate through the string of XHTML, tag by tag recursively, and create each javascript element on the page as I go, and then when I hit the placeholder, I create the slot at that point (which is easy to do in Vue.js: this.$slots.default
). I would rather not reinvent the wheel if I don't have to (making all the initial mistakes along the way) so if there's a method already out there and available or some kind of component that does this, that would be great. Otherwise, pointing me in the right direction would be invaluable. TIA.