var template = '<div class="entry"><h1>{{title}}</h1><div class="body">{{body}}</div></div>';
{{#repo}} <b>{{name}}</b> {{/repo}} {{^repo}} No repos :({ {/repo}}
var list = "<% _.each(people, function(name) { %> <li><%= name %></li> <% }); %>"; _.template(list, {people: ['moe', 'curly', 'larry']}); // "<li>moe</li><li>curly</li><li>larry</li>"
{{#list people}}{{firstName}} {{lastName}}{{/list}} { people: [ {firstName: "Yehuda", lastName: "Katz"}, {firstName: "Carl", lastName: "Lerche"}, {firstName: "Alan", lastName: "Johnson"} ] } Handlebars.registerHelper('list', function(items, options) { var out = "<ul>"; for(var i=0, l=items.length; i<l; i++) { out = out + "<li>" + options.fn(items[i]) + "</li>"; } return out + "</ul>"; });
var templateWrapper = document.getElementById('templateWrapper'); something.render(template, templateWrapper); var templateElemet = templateWrapper.getElementsByClassName('templateElement'); //
// var template = function(data){ // 'e' , div return {c: 'user', C: [ {e: 'a', h: 'http://facebook.com/' + data.facebookId, c: 'user-avatar-wrapper', C: {e: 'img', c: 'user-avatar', S: 'http://graph.facebook.com/' + data.facebookId + '/picture?width=200&height=200'} }, {c: 'user-name', t: data.fristName + ' ' + data.lastName, n: 'userName'}, ]}; }; // . tp var build = cnCt.tp(template, {fristName: 'swf', lastName: 'dev', facebookId: '100005155868851'}, document.body); // build.r.addEventListener('click', function(){console.log('hello')}); //build.r (r root) div.user build.userName.addEventListener('click', function(){console.log(this.textContent)}); // n: 'userName'
// function dataToStr(_date){ var date = _date instanceof Date ? _date : new Date(_date); return date.getDate() + '/' + date.getMonth() + '/' + date.getFullYear(); } // function l10n(key){ return l10Keys[key] || key; } var l10Keys = { youFriendsList: ' ', sendMessage: ' ' }, // templates = { // basis: function(){ return [ // {c: 'header', n: 'header', t: l10n('youFriendsList')}, {c: 'content-view', n: 'contentView'}, {c: 'footer', t: ' SoftWear LLC'} ]; }, // icon: {c: 'icon'}, // button: function(data){ return {c: 'button', t: data.t, n: data.n, C: templates.icon, a: data.a}; }, // friendsList: function(friends){ var items = [], i = 0, iMax = friends.length; // js ( , ) for(; i < iMax; i += 1){ items[i] = {c: 'friend', C: [ {e: 'a', h: 'http://facebook.com/' + friends[i].facebookId, c: 'friend-avatar-wrapper', C: // {e: 'img', c: 'friend-avatar', S: 'http://graph.facebook.com/' + friends[i].facebookId + '/picture?width=200&height=200'} }, {c: 'friend-name', t: friends[i].firstName + ' ' + friends[i].lastName}, // {c: 'friend-birthday', t: dataToStr(friends[i].birthday)}, // templates.button({n: 'sendMessage', t: l10n('sendMessage'), a: {'data-friend-id': friends[i].facebookId}}) ]}; } return {c: 'friend-list', C: items}; } }, // friends = [ { facebookId: 1, firstName: '', lastName: '', birthday: 1 }, { facebookId: 2, firstName: '', lastName: '', birthday: 111111111 }, { facebookId: 3, firstName: '', lastName: '', birthday: 123123123 } ], build, $contentView, $buttons, i; // ( ) cnCt.bindTemplates(templates); // $contentView = cnCt.tp('basis', document.body).contentView; function getByFacebookId(id){ var i = friends.length; id = +id; for (; i-- ;){ if (friends[i].facebookId === id){ return friends[i]; } } return null; } $buttons = cnCt.tp('friendsList', friends, $contentView).sendMessage; // .. function sendMessage(){ var friend = getByFacebookId(this.getAttribute('data-friend-id')); console.log('hello ' + friend.firstName); } for (i = friends.length; i-- ;){ $buttons[i].addEventListener('click', sendMessage); }
Source: https://habr.com/ru/post/202432/
All Articles