[Discussion] Use a template engine with backend integration instead of a fully-JS UI framework like react or angular?
Tags: javascript, angular, discuss, react
Published: 2021-06-12 05:09:39

Let me start by saying that I am a back-end developer. I do have some experience with front-end technologies, but I can't say that I am capable of a full-stack dev session. With that being said, let me rant a bit.

I find all the JS UI frameworks confusing. There are so many of them, each has its own syntax and structure, many components, and doesn't use "traditional" JS/TS, but many placeholders that get "replaced" later on. I reached this conclusion recently when I started a new project (with a new company) and the UI part was done purely in Angular and the UI communicated with the BE using REST calls. The project had less than a month since it started, so I wasn't quite from the start so I could not impose my view :D

This brings me to my next rant. The UI does soo many calls to the backend all the time. The project has a complex data structure so there are many back-and-forth messages from UI to BE to get certain data elements, save them, updated them, and so on. At one point, for one screen, I counted almost 60 calls from the UI just to populate the needed data.

### There HAS to be a better way!

Let the backend do the job
I've been using template engines for as long as I can remember. It offers so many advantages (at least for me) and I honestly can't see a reason why NOT to use one. FTL templates are well integrated with most BE Java frameworks (*cough* Spring *cough*) and it gives BE control over the data that is being shown and returned.

Need to show a certain page, let the BE do all the DB queries, filtering and only display the resulting data in one go. One call to the BE to get everything you need. Also, the risk of accidentally returning MORE data than needed is reduced, since everything that is returned is displayed on-screen (with a few exceptions). Instead, if UI communicates with the BE using REST calls only, you get a complex JSON structure back but maybe only a small portion is needed.

Back-end guys can work on the UI as well when a template engine is used. Let the FE team deal with actual styling, element positioning, design... you know, the actual template. The UI should not do business logic, so there is less work for them. You would say that there is more work for BE, but that is not totally true since a good back-end always validates data received, so we are already doing most of the work anyway.

Next, there is the security component. If only REST calls with JSON IN/OUT is done by the FE, you need to validate the request origin, maybe keep an updated table with all FE servers (in case of a distributed system with many instances), use a JWT maybe... all on top of the normal session-based validations. Just using a CSRF token becomes harder when the FE is so isolated from the BE.

### Using a template engine is not perfect either

I am not saying that all the problems go away when using a template engine that is controlled by the BE. There still needs to be a lot of work on the UI side and you can't truly make "intelligent" FE that actually do (well) some of the business logic.

Also, I find it *REALLY HARD* to find a modern UI framework that works well with this. I've been using Bootstrap, but most others (like Vue, React, Angular and Flutter) don't work with a backend-driven template engine. If you do know any good and modern ones, please let me know cause I am really struggling to find something.

### Discussions and Conclusions

So what is your take on this? I know I may get a lot of hate since there are soo many FE devs focused on react/angular, but I am sure that any BE dev feels, at least partially, what I feel.

Do you use or know any UI framework that works well with FTL? I really want to stop using jQuery as much as possible for my JS needs and maybe switch Bootstrap as well with something better.