Should I use Backbone and client templates like Mustache?

Recently, I get a similar feeling. I felt this way a few years ago when I used exclusively Apache, Django and MySQL. I felt that the web stack I was using began to show my age. So then I switched to Nginx, Flask and MongoDB. I am starting to feel that this stack is again showing its age, because I hear about tons of new cool things that sound easier in new stacks.

For example, I hear a ton about Backbone.js, as well as client-side templates such as Mustache. It seems like they can use URL hashing and create some pretty awesome sound apps.

I played with Node.js, Express.js and Mustache.js for templates (I don't like jade). This setting is similar to what I can get to know, because it reminds me of Jinja, which I used with Flask. This still does not include Backbone or client-side templates. But how do I know which good script to use when I see it? Is Backbone and Mustache on the client something I should invest time on? How do I know when I will use them and when I will not?

If I switch to this newer stack, I think that I will still generate my HTML on the server in the same way as I do with my current stack. How to make the transition to client templates and why, I think?

+4
source share
4 answers

you probably won’t get a clear answer to such a question, it’s a more open, and not a real problem with the code, you are more likely to move to another place. but so far this will not stop me from giving an answer.

  • there is nothing wrong with generating html on the server, even sites with huge javascript injections like google + etc. still display most of the markup on the server.

  • the use of the highway, in my opinion, depends on which site / application you want to do, it already says it, the first big thing that I ask myself, do I make an application? or website. while some sites may benefit from backbone networks, I believe the app is where the spine shows in all its glory. When creating a website, I would use a basic framework for some structure, but not for all transaction models, collections, views, and routers.

  • on the template engine you need to choose your engine for two reasons:

    • I like to write how this engine works (syntax, not too much javascript inbetween my html (loops, etc.))
    • maybe my engine can do what I want it to do (in some cases you want nested templates, some engines do not, ...

the former is less important than the latter, the syntax can often be changed, some engines support a configuration in which you set the syntax as {{}} or <% =%> or something that you draw on yourself.

+4
source

Use backbone.js ...

  • when you build a medium to heavy web application,

  • and only when you have a restful api running that returns json

Use client-side templates ...

  • when you can populate them with data from a json object,

  • and you want to use caching of the layout and structural content on the client and receive only data from the server

+4
source

This is not a technical answer like the other answers, but it will give you different perspectives.

You should definitely use Backbone or another client-side platform, because speed matters to the user. The faster the experience, the more time people will spend on your site. People do not like to wait, and people are more engaged when something is fast. There are a few things that kill an engagement faster than waiting.

Why the highway?

You are already familiar with MVC, so this is nothing new. With an MVVC framework like knockout, you have to spend time looking for this template.

This is one of the most popular client frameworks. Many people use it, and there are tons of resources to help you learn. I gave a list of companies using it in production: http://www.quora.com/Can-we-get-a-poweredby-list-of-sproutcore-javascriptmvc-backbone-js-individually

Since this is the most popular client structure compared to others (Sproutcore / Amber.js, Spine, Knockout.js, Cappucino, etc.), there is a lot of documentation.

As for the template engine, I recommend you not to worry about anything other than John Resig micro templates, which are already part of underscore.js (which is the only hard dependency except jQuery or Zepto.js). Raise another template engine - another moving part, to worry about the fact that it really is not so important. It is also trivial to switch from one template engine to another. Switching is half a day for a two-day project for a small to medium sized project.

+1
source

I think I can see why you are doing this. Personally, I think NodeJS, ExpressJS, MongoDB, and MustacheJS work well together.

You can create a web page as usual. Use MustacheJS to place the fields in which you want the data to appear in the markup.

Then, on the NodeJS side, you can use express to route to the html file, collect data from mongoDB, use mustacheJS to parse the file, and then push it to the client.

This is pretty straight forward.

Here is an example of what I wrote not so long ago:

NodeJS, ExpressJS, and MustacheJS Templates

As for the data, I use a standard object, but it can be easily replaced with the mongoDB dataset.

+1
source

Source: https://habr.com/ru/post/1386600/


All Articles