Skip to content

Instantly share code, notes, and snippets.

@tbuyle
Last active May 23, 2024 23:33
Show Gist options
  • Save tbuyle/b72d548fa03ec040780bb5448e9fd8e2 to your computer and use it in GitHub Desktop.
Save tbuyle/b72d548fa03ec040780bb5448e9fd8e2 to your computer and use it in GitHub Desktop.
Load external HTML template and append them to the document body (Stimulus.js)
import { Controller } from "stimulus"
export default class extends Controller {
connect() {
const templates = this.element.import.querySelectorAll("template");
templates.forEach(function(template) {
document.querySelector('body').appendChild(template);
});
}
}
<html>
<head>
...
<link rel="import" href="/path/to/template.html" data-controller="html-template">
</head>
<body>
...
</body>
</html>
<template>
<div>
Template Content
</div>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment