Benefit of using template engine

2

I'm creating cells for tables getting information from an api using append, I wonder if there would be any benefit in using some template engine like Handlebars.js instead of using append?

code:

function ajaxRank(){
  var $orders = $('#orders');


  $.ajax({
    headers: { 'X-Auth-Token': '69e49cf35c7346fcb819f023cf0b98d4' },
    url: 'http://api.football-data.org/v1/soccerseasons/398/leagueTable',
    dataType: 'json',
    type: 'GET',
  }).done(function(response) {
    var regex = /.*?$/;
    for (i = 0; i < 20; i++) {
      $('#tbodyRank').append('<tr>' + '<td>' + response.standing[i].position + '</td>'
      +'<td><img id="logo" src="../style/img/' + response.standing[i].teamName +'.png"' +  'alt="description here" /></td>'
       + '<td>' + response.standing[i].teamName + '</td>' +
      '<td>' + response.standing[i].playedGames + '</td>'+
      '<td>' + response.standing[i].points + '</td>' +
      '<td>' + response.standing[i].goals + '</td>' +
      '<td>' + response.standing[i].goalsAgainst + '</td>' +
      '<td>' + response.standing[i].goalDifference + '</td>' +
      '<td>' + response.standing[i].wins + '</td>' +
      '<td>' + response.standing[i].draws + '</td>' +
      '<td>' + response.standing[i].losses + '</td>' + '</tr>' );
    }
  });
}


module.exports = ajaxRank;
    
asked by anonymous 25.03.2016 / 00:40

1 answer

4

Your question is very relative because it really is a matter of taste (and opinion) the difference is really in quality and organization you want for your code.

Application Architecture & Hierarchy of Information

I have no idea how your application works so I can not diagnose whether or not it is better for you to use handlebars.js , but here are some questions you should ask yourself, so that you can evaluate if it is viable Template Engine :

  • Are you following any Framework ? If your answer is YES, take a look at its documentation and see how the community organized the project.

If we were to follow an example that probably does not apply in this case but is good for knowledge, we have the famous Laravel Framework , which is a PHP Framework based on Standard MVC architecture, from which briefly, separates the application into layers having the:

  • Model - Responsible for interacting directly with the Database working with tables, columns and etc ...
  • View - View is nothing more than what we (users) see and interact with on the page that would be equivalent today to HTML, CSS and JavaScript .
  • Controller - Interfaces between View and Model , it is in Controller that you do verification, sanitization of the inputs , it is there where the Model the data will be passed and etc ...

Read more about the Model-View-Controller Architecture Standard by going to the following link link .

  • Do you think using handlebars.js will make your code more readable and easy to maintain?

  • Is this dependency easy to implement in the project, without many (or no) headaches?

Conclusion

In my opinion a good developer not only knows how to "coddle" very well or handle a lot of programming language either Front-end or Back-end. But a good developer knows what is best for his project / application being impartial and not favoring some technology just because he thinks (just by thinking) that he is the best.

If I come here and tell you that it is better for you to use handlebars.js I would be lying, just as if I also told you otherwise.

    
25.03.2016 / 01:57