Reputation: 33
Right now, when a button is clicked on my page, I want a div to be populated with a lot of HTML-- a small form's worth. The strategy I'm using now implements usage of the .html() JQuery method which passes in this behemoth string:
'<div class="container-fluid">' +
'<div class="row"> </div>' +
'<div class="row"> </div>' +
'<div class="row"> </div>' +
'<div class="col-md-3 col-md-offset-1 col-sm-6">' +
'<select id="measures" class="form-control">' +
'<option>Select a Measure</option>' +
'</select>' +
'</div>' +
'<div class="col-md-3 col-sm-6">' +
'<select id="budgets" class="form-control">' +
'</select>' +
'</div>' +
'<div class="">' +
'<button class="col-md-1 text-center col-sm-2 btn btn-primary" id="createSMSButton">Create Summary</button>' +
'</div>' +
'<div class="">' +
'<button class="col-md-2 col-md-offset-1 col-sm-3 btn btn-primary" id="createBudgetReportButton">Create Budget Report</button>' +
'</div>' +
'<div class="row"> </div>' +
'<div class="row"> </div>' +
'<div class="row"> </div>' +
'</div>';
I don't really like the fact that this string is so bug-prone. I also don't really like the idea of requesting a new .cshtml file from the server, which would take up server/ network time, and force users to wait for an AJAX request to happen. Is there a way to add all this HTML to a div without use of a string this long and delicate and/or slow AJAX requests?
Upvotes: 3
Views: 613
Reputation: 4971
Since you don't want to use Ajax (which would allow you to have templates in separate files), what you can do is storing your html in a tag with type="text/html"
and retrieve it accessing the innerHTML
property of the script element.
<script id="html" type="text/html">
<div class="container-fluid">
<div class="row"> </div>
<div class="row"> </div>
<div class="row"> </div>
<div class="col-md-3 col-md-offset-1 col-sm-6">
<select id="measures" class="form-control">
<option>Select a Measure</option>
</select>
</div>
<div class="col-md-3 col-sm-6">
<select id="budgets" class="form-control">
</select>
</div>
<div class="">
<button class="col-md-1 text-center col-sm-2 btn btn-primary" id="createSMSButton">Create Summary</button>
</div>
<div class="">
<button class="col-md-2 col-md-offset-1 col-sm-3 btn btn-primary" id="createBudgetReportButton">Create Budget Report</button>
</div>
<div class="row"> </div>
<div class="row"> </div>
<div class="row"> </div>
</div>
</script>
var html = document.getElementById("html").innerHTML;
Another option is to use RequireJs with text plugin https://github.com/requirejs/text to load templates as a dependency of your modules.
define(['text!/templates/content.html'], function() {
/*...*/
});
Behind the scenes RequireJs will make an Ajax request to download your template, but I think you should consider the benefit in your application mantainability having separate files for templates.
Additionally you could use the RequireJs optimizer tool to bundle all the dependency in a single file and avoid the extra calls.
Upvotes: 3