0

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If I were to retrieve information from a JSON AJAX request, how would I put the information in there?
You'd use some sort of template engine. Underscore. KO (parts). Handlebars.
0

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.