Skip to the content.

Suppose you have a variable, message, that you want to be displayed, along with today’s day, in the markup generated by Dompiler. You can do so with this JavaScript (which you’ll place in a file called app.js):

// Import Dompiler.
import Dompiler from "../../library/dompiler.js";

// Initialize Dompiler.
let {
    } = new Dompiler().withBinding();

// Variables.
let message = "Hello World.",
    date = new Date().toLocaleDateString();

// Compile markup string into a document fragment.
let markup = `
            ${message} The date is ${date}.
    compiled = compile(markup);

// Append compiled markup to container.
let container = document.querySelector(".example-container");

Note that this is really just using template literal interpolation syntax to inject the variables into the string. This is an example of how Dompiler works in tandem with plain JavaScript. You can read about template literals and interpolation here: A Guide to JavaScript Template Literals

The markup would look something like this:

<!DOCTYPE html>
    <title>Dompiler Example</title>
    <div class="example-container"></div>
    <script src="app.js" type="module"></script>

You can see a live example here: