I also like the idea of ββusing a template tag, and I tried to make underline patterns in the html5 template element in various ways. Unfortunately, the element specifically means the html template, and the content will be converted to a document fragment that is not suitable for many valid underline templates, even if they subsequently display valid html.
Therefore, the only use I can offer is that you could save the script elements organized inside the template element as follows:
<template class="underscore-templates"> <script id="new-email"> <div class="email"> <div class="timestamp"> <%= received %> </div> <div class="from"> <%= from %> </div> <div class="title"> <%= title %> </div> <div class="message"> <%= message %> </div> </div> </script> <script id="other"> </script> </template>
And then they are separated to safely do things like:
var templates = $('.underscore-templates').prop('content'); _.template($(templates).children('#new-email').html(), {...});
with a template element that serves as a scope to prevent common problems with an identifier conflict and to execute these templates as scripts.
(However, this will be limited to modern browsers without a thorough study of how (or maybe if) you can get the contents of the template elements in older browsers and make it searchable by snippet.)
source share