Mark Eriksson
Mark Eriksson

Reputation: 1475

How to use JavaScript templates?

As I go around on certain websites, I often view the source code, and on a few websites I've come across 'script templates'. They seem pretty good and rather easy to implement.

How would I go about using them? Can't really find any decent tutorials on the Internet.

Thanks.

Upvotes: 0

Views: 244

Answers (2)

Bny
Bny

Reputation: 41

I recommend Enfusion Framework for perfect HTML templating. You can template different dataTypes

http://www.enfusion-framework.org/

Upvotes: 0

ZenMaster
ZenMaster

Reputation: 12742

Fairly simple. You define a script tag with an unknown type, normally something like this:

<script type="text/html" id="myTemplate">
   <div>
      ...
   </div>
</script>

Then you put it in your HTML.

Browser will ignore the content of the script for rendering purposes and won't try to execute it due to the non-standard type.

Then you just retrieve it by ID document.getElementById('myTemplate') and do what you please.

Upvotes: 3

Related Questions