Skip to content

Instantly share code, notes, and snippets.

@KargJonas
Last active December 16, 2018 16:40
Show Gist options
  • Select an option

  • Save KargJonas/a484add4ce17f072df7a1c74a6080cd3 to your computer and use it in GitHub Desktop.

Select an option

Save KargJonas/a484add4ce17f072df7a1c74a6080cd3 to your computer and use it in GitHub Desktop.
The Smallest "Framework"

This is all that is needed:

function inst() {
	Array.from(document.getElementsByTagName("comp")).map(element => {
		if (!element.hasAttributes()) return;
		const func = element.attributes[0].name;
		if (!/\w\(.*\)/.test(func)) return;
		element.innerHTML = eval(func);
	});
}

window.addEventListener("load", inst);

It allows you to insert the returned values of functions directly into HTML.

An example:

<body>
    <comp test([0,1,2,3])></comp>
    <comp test(["Marcus","Lucas","Ben"])></comp>
	
    <script>
        function test(numbers) {
            return numbers.map(
                num => `<h1>${num}</h1>`
            ).join("");
        }
    </script>
    <script src="inst.js"></script>
</body>

When inst() is called every component is "rendered". Components can be renderd as often as you want.

This has many disadvantages - for example that any arguments passed into the component are lowercase or that you won't get an output if you have spaces in the function argument. But it's still kindof cool and potentially useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment