2

I've been using this nifty trick to store blocks of metadata, be it JSON or fragments of HTML:

<script type="text/html">
    <p>This gets something done to it later.</p>
</script>
<script type="application/json">
    {"name":"Niet","species":"Absol"}
</script>

JavaScript then gets these tags and uses their contents in some way.

This works great. I only have one problem...

<script type="text/html">
    <p>This is a template for something used in many places.</p>
    <p>Rather than having, say, a hundred copies of basically the same thing,
        this lets me define the HTML once and then re-use it as needed,
        while allowing for easy editing because it doesn't need mass escaping.</p>
    <p>However, what if I want a template that contains a template?</p>
    <script type="text/html">
        <p>Nested scripts?</p>
        <p>That's not gonna work.</p>
    </script>
</script>

So what do? Is there another tag, maybe a new HTML5 feature I haven't heard of yet, that lets me define template and other data in this way?

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • There's the html5 [template](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template) tag, but it doesn't look like Microsoft browsers will support it any time soon. If you only need one level of nested script tags, maybe you could put everything in an HTML comment, as suggested [here](http://stackoverflow.com/a/13886336/2759272) and [here](http://stackoverflow.com/a/17283220/2759272). – DoctorDestructo Sep 14 '15 at 18:50
  • @DoctorDestructo Commenting it could definitely work, although it will end up nuking my editor's colour coding... unless I do something like `=""?>` so the IDE sees it as a PHP string rather than an HTML comment... Feels so haxy though, but if it works... XD – Niet the Dark Absol Sep 14 '15 at 19:05

0 Answers0