Been doing today for years with a custom tags. name it anything you want. works
Templating in HTML
51–60 of 78 posts
Re: Templating in HTML
#52is great, but some people are surprised that it doesn't include any form of parameterization or expressions. The entire process of cloning a template and updating it with data is left up to the developer - it's pretty low level. That's why my team made the lit-html library, which uses JS tagged template literals to make elements for you, clone them and interpolate data where the JS expressions are, and then update th…
Do you have a link to the template instantiation proposal?
https://github.com/WICG/webcomponents/blob/gh-pages/proposal...
Perhaps there are more recent versions.
I liked the spirit of the proposal, but never studied it.
Re: Templating in HTML
#53Sorry, but the tag doesn't actually do "templating in HTML" ...but it should. It's shocking to me that we've had the modern paradigm of client-side-rendering frameworks for over a decade now, without so much as an RFC for any kind of native support. For the sake of render performance, bundle size, removing a need for transpilation, compatibility across frameworks. There are a million reasons this should be happening…
Re: Templating in HTML
#54 1. Define Template using ...
2. Consume Template using ..
With substitution variables/text, this feature could be amazing.Re: Templating in HTML
#55The tag really shines when used alongside and the Shadow DOM. But I really wish there were an HTML-native way to load from separate files, the same way we do with CSS and JS. Not a show-stopper, but it’d be very nice to have.
> But I really wish there were an HTML-native way to load from separate files Seems like the JS folks blocked this from happening. It's weird to me to think we've arrived at the point where JS considerations have come to dominate for something that was built to be a document sharing platform.
Not JS. Chrome, and their decade-old crusade to make web components happen, no matter the cost or common sense
Re: Templating in HTML
#56I wish HTML and not just JS had supported for consuming templates. That is I wish we could do variants of: 1. Define Template using ... 2. Consume Template using .. With substitution variables/text, this feature could be amazing.
At which point, are you just re-creating JavaScript?
Re: Templating in HTML
#57I wish HTML and not just JS had supported for consuming templates. That is I wish we could do variants of: 1. Define Template using ... 2. Consume Template using .. With substitution variables/text, this feature could be amazing.
const $template = function(template) {
// make copy of template content
const root = template.content.cloneNode(true);
// create proxy object for accessing named nodes and root
const obj = {
get $root() {
// after template root is called, remove this getter function
delete obj.$root;
// return root only once
return(root);
}
};
// find all named template nodes and add to proxy object
for (const node of root.querySelectorAll('[data-tmpl-name]'))
obj[node.dataset.tmplName] = node;
// otherwise create template node content wrapped in proxy which
// makes attempts to overwrite properties an error.
return(new Proxy(obj, {
set: () => { throw (new Error(`Attempt to overwrite a template node!`)); }
}));
}
You can use it with something like:
And then just: function of_some_sort() {
const t = $template(document.getElementById('some_id'));
t.header.innerHTML = 'The Header';
t.title.innerHTML = 'The title';
t.info.innerHTML = 'More stuff.';
document.body.append(t.$root);
}
This is was the first version I made. It's not hard to get from here
to a version that can fill the template with a data object for you. In
my most recent version, you could do: function of_another_sort() {
document.body.append($template(document.getElementById('some_id'), {
header: 'The Header',
title: ['first title', 'second title'],
info: (elem) => {
elem.setAttribute('functions', 'work');
elem.innerHTML = 'and receive the internal element itself.';
}
}));
}
Arrays duplicate the internal element and make copies of it, objects
recurse into the element building a 'key.path.name' while looking for
data-tmpl-name to replace. Functions get a copy of the element for
more than just innerText/innerHTML replacement. A null or false value
eliminates the internal element, and a true value passes it through
unchanged.If you've ever used the old ruby library Amrita, it's basically that, but for HTML Template elements. You can easily do all this in about 100 lines of JS.
Re: Templating in HTML
#58
I'll definitely have in mind from now on.Thank you!
Re: Templating in HTML
#59Sorry, but the tag doesn't actually do "templating in HTML" ...but it should. It's shocking to me that we've had the modern paradigm of client-side-rendering frameworks for over a decade now, without so much as an RFC for any kind of native support. For the sake of render performance, bundle size, removing a need for transpilation, compatibility across frameworks. There are a million reasons this should be happening…
For server-side rendering, SGML (and also some other template "engines") provide HTML-aware, type-checked, parametric macro expansion. Actually, SGML templating works transparently on the client side and the server side.
Within the browser OTOH, there's already JavaScript, making every dynamic feature added to HTML inessential for better or worse, like it has for over 25 years now. That's just how it was decided a long time ago, and adding half-assed features to HTML like the template element (which requires JavaScript, in turn, thus doesn't add any essential capability) all the time is exactly the thing we shouldn't be doing when the damn "web stack" is already bloated as fuck.
Re: Templating in HTML
#60I've recently used that on an interview project I did (https://github.com/pretzelhands/ubiquitous-sniffle) and it surprisingly takes you quite far with very little effort.
The only major annoyance is really manually keeping track of the elements in the DOM and .innerText and .innerHTML-ing everything that needs a dynamic value. But it's manageable if you keep it confined.