Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do browser-side javascript template engines render templates?

I found a large number of browser-side JavaScript templates which render a template with data values a string. However, I'm a bit uncertain how this string is supposed to be translated to display elements in the DOM.

Obviously, one way is just document.write - but I think that has been long buried since.

Another is using element.innerHTML, but atleast some years ago this was not part of any standard, and it didn't work for XHTML-documents.

So, what's the deal nowadays? Is .innerHTML the one to use and that works perfectly for XHTML and is a part of a standard? Or do people just use it and it works? Or is there something else that is supported?

As an aside, PURE.js seems to be all about building templates out of DOM-elements, which seems pretty attractive to me - are there any other template engines which work the same way?

like image 885
Nakedible Avatar asked Jun 14 '26 22:06

Nakedible


1 Answers

innerHTML works pretty much everywhere.

These methods are also useful for working with the DOM which is basically what a template engine needs to do:

  • createElement
  • appendChild
  • createTextNode
  • removeChild
  • parentNode

libraries like jQuery make extensive use of these.

like image 165
Moin Zaman Avatar answered Jun 16 '26 12:06

Moin Zaman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!