Horváth, Győző
senior lecturer
horvath.gyozo@inf.elte.hu
Financed from the financial support ELTE won from the Higher Education Restructuring Fund of the Hungarian Government
Abstraction for generating HTML
Concatenation
function createListItem(book) {
return '<li><a href="show/' + book.id + '">' +
book.title + '</a></li>';
}
<script id="listItem" type="text/template">
<li>
<a href="show/<%= id %>"><%= title %></a>
</li>
</script>
//Getting the template string
var templateString = $('#listItem').html();
//Compiling the template
var compiledTemplate = _.template(templateString);
//The data
var book = {
id: 12,
title: 'JavaScript Patterns'
};
//Generating HTML string
var htmlString = compiledTemplate(book);
//Inserting HTML string to the document
$('#viewWrapper').html(htmlString);
In the previous application generate the list items with a template!
https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js
<script id="listItems" type="text/template">
<% titles.forEach(function (e) { %>
<li class="list-group-item"><%= e %></li>
<% }) %>
</script>
deleting makeListItemsFromArray
class ListView {
constructor(props) {
/* ... */
this.template = _.template( $('#listItems').html() );
/* ... */
}
onKeyUp(e) {
/* ... */
$('#code-list').html(this.template({
titles: filteredTitles
}));
}
}