R
R
romaro2021-11-24 22:37:09
Template Engines
romaro, 2021-11-24 22:37:09

How to correctly pass urls to the template engine during server rendering?

I am developing a site based on express, without using front-end frameworks. Suddenly stalled at a fairly simple moment. Since I'm server-rendered, in order to render the page to the user, I call the res.render method, which I pass in the template and an object that contains the values ​​for the substitutions.

The templates have placeholders for static URLs (all sorts of menu items, etc.), they look something like this:

<a href="{{ urls.auth.signup }}" class="_btnReg __btnReg">{{ static.signup }}</a>


That is, when starting the application, I'm going to create a global freeze-object urls, which will contain all the urls sorted by modules. For example:
urls: {
  auth: {
    signup: '/signup',
    }
}


This object can eventually grow to several hundred urls.

Accordingly, with each call to res.render, I plan to include it in the general list of placeholders. Without any filtering, stupidly all the urls of the project.

Of the alternative options, I see only two:
1) manually write urls in the template;
2) substitute urls at the template assembly stage with webpack, which, as it were, takes out part of the business logic to the assembler and is also not happy.

Can my approach be optimized? For example, I am thinking about filtering in order to pass only those static links that are on the page to the template, but I don’t understand how to approach this functionality, and is the game worth the candle.

In general, it will be interesting to know how such a problem is solved in more or less serious projects.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question