I
I
It2021-06-09 19:31:08
Node.js
It, 2021-06-09 19:31:08

How to properly create and export data array in node js?

Good afternoon, there is an application skeleton on express.js + EJS, page templates have been created that have elements that are different on each page.
Given via <%= locals.title %>.

Each page has its own file for assigning all template variables. This file only

module.exports = {
  title: 'Страница 1',
}

Three questions for the node gurus:
1. Is the template correctly set to access the variable (through locals) or is it necessary somehow differently?
2. How can I use data arrays to pass to the template?
For example, there is a variable block with logos of satisfied customers, but the set of logos should be different for different pages.
In theory, somewhere you need to create an array with customer data, such as
clients = {
    data: [
        {
            logo: "im.svg",
            title: "Client1",
            clientId: 'id1'
        }] 
}

And then in the file to the page, specify only the necessary client IDs, and in the template itself, output them in a loop.
Accordingly, where to create such an array and how to be able to access different fields of each object from the array in the template? So that in addition to the logo, you can also add a name, for example.

3. How to include some code from an external file in module.exports? For example, some logos must be used in the inline svg format, files with svg code are created for them and they are inserted into the template simply through <%= include() %> или <%- partial() %>, but how can they be transferred to the template via module.exports?

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