Benefit from the use of template engine

Asked

Viewed 84 times

2

I’m creating table cells by grabbing information from an api using "append", I wonder if there would be any benefit to 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;

1 answer

4

Your question is very relative because it does not matter in reality, it is a matter of taste (and opinion) the difference is even in the quality and in the organization that you want for your code.

Application Architecture & Information Hierarchy

I have no idea how your application works so I can’t diagnose if it’s better or not for you to use the handlebars.js, but here are some questions you should ask yourself, so only then assess whether it is feasible to use some Template Engine:

  • Are you following any Framework? If your answer is YES, take a look at the documentation of it 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 MVC Architecture Standard, from which it briefly separates the layered application having the:

  • Model - Responsible for interacting directly with Database working with tables, columns and etc...
  • View - The View nothing else is what we (users) see and interact on the page that would be equivalent today to HTML, CSS and Javascript.
  • Controller - Make the middle between View and the Model, is in the Controller that you do the checking, sanitizing of inputs, is there where it is set to which Model the data will be passed on and etc...

Read more about the Architecture Standard Model-View-Controller by accessing the following link http://blog.thiagobelem.net/o-que-e-o-mvc.

  • You think by using the handlebars.js will make your code more readable and easy to maintain?

  • Such a dependency is easy to implement in the project, without many (or no) headaches?

Completion

In my opinion a good developer not only knows "codar" very well or much of some programming language is Front-end or Back-end. But a good developer knows what’s best for his project/application by being unbiased and not favoring some technology just because he thinks (just because he thinks) it’s the best.

If I come here and tell you you better use the handlebars.js I’d be lying, just as if I told you otherwise.

  • If the answer was helpful to you, I’d be happy if you could mark it as the right one.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.