Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I correctly store a HTML template within a HTML page?

I have some small template strings, which will be rendered through Mustache.js on the same page.I need not create seperate html files for templates.

Options for storing the templates :

  1. Storing in javascript variables : Hackish multiline strings, lots of escaping of quotes.

  2. Storing as innerHTML of hidden divs.

I tried method#2, but it does not seem to work correctly.

Fiddle: http://jsfiddle.net/RHwnq/2/

<html>
<head></head>
<body>
<div id='templates' class='hide' align="">
         <div id='tableTemplate'>
            <table class="table">
                 {{#name_list}}
                  <tr><td> {{name}} </td></tr>
                {{/name_list}}
            </table>
         </div>
</div>

<script>
 var template = $('#tableTemplate').html();
 console.log(template);
</script>

</body>
</html>

This logs :

{{#name_list}}
  {{name}} 
{{/name_list}}
<table class="table"><tbody><tr><td></td></tr></tbody></table>

Instead of :

  <table class="table">
                     {{#name_list}}
                      <tr><td> {{name}} </td></tr>
                    {{/name_list}}
  </table>

This might be to due to some markup correction by the browser. What are other good tricks to store HTML templates within an HTML page ?

like image 959
DhruvPathak Avatar asked Nov 28 '12 11:11

DhruvPathak


People also ask

What is the use of template in HTML?

HTML &lt;template&gt; Tag 1 Definition and Usage. The <template> tag is used as a container to hold some HTML content hidden from the user when the page loads. 2 Browser Support 3 Global Attributes. The <template> tag supports the Global Attributes in HTML. 4 More Examples. Fill the web page with one new div element for each item in an array.

How to open HTML templates in Google Chrome?

Most HTML templates come in a zipped file. Go to the folder where your template has just been downloaded and extract it. Find ‘index.html’ or ‘index.htm’. In our example, index.html file is in Noah-master directory after extracting the zip file. Now open the file in any browser, for instance- Google Chrome.

How do I create HTML code without the <template> tag?

To do this without the <template> tag, you have to create the HTML code with JavaScript to prevent the browser from rendering the code. The <template> tag supports the Global Attributes in HTML.

How to render content inside a <template> tag?

Content inside a <template> tag will not be rendered. The content can be made visible and rendered later by using JavaScript. Use the <template> tag when you have HTML code you want to use over and over again, but not until you ask for it.


2 Answers

I store them in a script tag, so they don't get rendered, like this:

<script id="abc-template" type="text/html">
    <h1>{{name}}</h1>
</script>

You can then reference them like this:

var template = $('#abc-template').html();
var html = Mustache.to_html(template, data);
$('#abc-div').html(html);
like image 92
Ross McNab Avatar answered Nov 06 '22 21:11

Ross McNab


I'd use HTML "template" tag.

<template id="tmp">Some HTML here</template>

See this example:

https://jsfiddle.net/cityremade/xwLht8vc/

like image 35
cityremade Avatar answered Nov 06 '22 21:11

cityremade