GitXplorerGitXplorer
s

notemplate

public
22 stars
1 forks
0 issues

Commits

List of commits on branch master.
Verified
52c21f5336ed1abb5ada0e81474f86bca9c5f1cd

Using DocumentFragment

ddoekman committed 3 years ago
Verified
9d743784f0469d0856a6ef7771ee146783de2d95

Update notemplate.js

ddoekman committed 3 years ago
Verified
28397426c2c89866d4454523754d3569900aabc2

First try

ddoekman committed 3 years ago
Verified
f8e63e3be5a03b70774d911c9a46d4ed5a2713f3

Merge pull request #5 from doekman/tag-helpers

sstefanhaustein committed 3 years ago
Verified
1750243c9a61a873b56f25531bdabd1a0748884c

Minor edits

ddoekman committed 3 years ago
Verified
c61255554fbf9218663af0081b0a66dc496fcd9f

Made tag.name be the preferred form

ddoekman committed 3 years ago

README

The README file for this repository.

NoTemplate.js

After working on html templating for ~10 years, I think the best template library is no template library.

NoTemplate.js provides only two functions, tag() and raw(). Use nested tag() calls to build a DOM tree in pure JS in a concise and safe manner without any special syntax, compiler or preprocessor; raw() can be used to mark "safe" raw HTML content that won't be escaped.

The first parameter of tag() is the element name. Each HTML5 element also has a helper function registered on the tag function, so instead of using tag("p", ...), one can also use tag.p(...).

Additional parameters build the content:

  • Strings are safely added as text nodes. No need to escape anything.
  • DOM Nodes (in particular elements) are added as child nodes.
  • Properties of nested plain objects are treated as
    • Attributes where the value is a string
    • Boolean attributes where the value is a boolean (empty string for true, absence otherwise)
    • Event handlers where the value is a function
    • For "style", the value can be an object, which is translated to style properties
    • For "class", the value can be an array
  • Arrays are automatically flattened to simplify building from fragments.
  • Null values are ignored to simplify handling conditional children

For components, create functions that return elements.

Is this a Joke?

As much as vanilla.js: I am using this myself for quotations.ch to simplify "old-school" HTML construction from JavaScript, replacing direct use of the DOM API. For larger projects, it might make more sense to use something like vue.js to decouple rendering and state management. I hope this project might help people to "get going" with small projects without pulling in half the world as dependencies.

Use Cases

Let me know if I should add anything here.

Simple Example

document.body.appendChild(
  tag.p(
    "Hello ",
    tag.a({
        href: "#",
        clickevent => alert("Hello")},
      "World")));

TodoMVC Implementation

  • Source Code (< 150 LOC; the view code starts around line 56)
  • Live Demo 
  • Should be one of the fastest loading readable / unobfuscated implementations: 

Developer Tools Network Tab