I’m having trouble finding a quick solution to add JS (Vanilla / jQuery / Angular) / HTML… I’d love any guidance to help find the best solution.
Goals are…
Add modular content quickly
Manageable Troubleshooting
Dependency Issues (paths, undefined etc)
Security
Ideally (but unlikely) just writing “include content.html”
or "import js-files.html"
would render correctly...
---- Just a few things I’ve tried -------
Gulp
I have a custom gulp setup for production builds but I’m having issues with paths and troubleshooting using this route.
Angular Includes
<div ng-include="'includes/content.html'"></div>
PHP requires…includes
<?php require_once("../includes/content.php");?>
HTML5 Templates / Imports
template.html
<template>
<misc-content><misc-content>
</template>
index.html
...
<link rel="import" href="html/template.html ">
</head>
<body>
<section id="section">
<script>
var link = document.querySelector('link[href="html/template.html"]');
var template = link.import.querySelector('template');
var clone = document.importNode(template.content, true);
document.querySelector('#section').appendChild(clone); // link to div or section ID on main page you want it sent to
</script>
...
</body>
HTML Shadow DOM & Custom Elements
-----------------------------------
*import.html*
<template id="temp">
<style>
::content > * {
color: red;
}
</style>
<span>I'm a shadow-element using Shadow DOM!</span>
<content></content>
</template>
<script>
(function() {
var importDoc = document.currentScript.ownerDocument;
var proto = Object.create(HTMLElement.prototype);
proto.createdCallback = function() {
var template = importDoc.querySelector('#temp');
var clone = document.importNode(template.content, true);
var root = this.createShadowRoot();
root.appendChild(clone);
};
document.registerElement(�?custom-element', {prototype: proto});
})();
</script>
index.html
....
<link rel="import" href="html/import.html ">
</head>
<body>
<custom-element></custom-element>
</body>
...
Any help would be greatly appreciated.
html
like this:<object type="text/html" data="partial.html" ></object>
. In this partial you could also include scripts that would get loaded when the partial does.