0

So I've seen handlebars templates do this:

<script id="some-template" type="text/x-handlebars-template">  </script>

and am wondering if there's a way to use underscore.js templates like this. It would be really nice to have my templates in markup instead of sitting inside a string.

6
  • 1
    Why not just use handlebars? (Caveat: I love handlebars.) Commented Feb 6, 2013 at 20:28
  • 1
    You can use that script tag trick for anything you want. Commented Feb 6, 2013 at 20:30
  • @Pointy care to elaborate? Commented Feb 6, 2013 at 20:35
  • That trick is just a way of getting content into the DOM without being parsed as HTML (or anything else). You can get the contents of the script via .innerHTML and use it for anything, including any template library of your choosing. Commented Feb 6, 2013 at 20:37
  • @Pointy care to answer with an example or a resource I could at? Commented Feb 6, 2013 at 20:41

1 Answer 1

2

Sure, you can do this easily.

var templateText= document.getElementById('some-template').innerHTML;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.