M
M
Michael R.2021-01-19 12:47:22
JavaScript
Michael R., 2021-01-19 12:47:22

How to correctly pass values ​​to a template in Webpack 4?

Greetings!

It is necessary in Webpack 4 to pass a parameter to the template.

1. Googled the following implementation: https://github.com/jantimon/html-webpack-plugin/bl...
2. Changed the assembly in accordance with the example above, got the parameter in the template, everything is ok.

webpack.config.js:

new HtmlWebpackPlugin({
  template: 'src/page_landing_1.ejs',
  filename: "page_landing_1.html",
  templateParameters: {"prop": true},
}),

page_landing_1.ejs:
<% if(prop) { %>
<h1><%= prop %></h1>
<% } %>

Question 1 : how correct is it to pass parameters to the template? What implementations are better?
Question 2 : if it is necessary to pass dynamic parameters? In this case, the parameters are passed once at server startup, in order for the parameters to change, you need to restart the server ...
Question 3 : with this implementation, the connection of other templates (header and footer) to the current template falls off. What to do?

Downloaded tutorial https://ejs.co/ :
page_landing_1.ejs:
<%- include('header'); -%>
<% if(prop) { %>
<h1><%= prop %></h1>
<% } %>

Got an error:
ERROR in ./src/page_landing_1.ejs (./node_modules/html-webpack-plugin/lib/loader.js!./src/page_landing_1.ejs)
    Module build failed (from ./node_modules/html-webpack-plugin/lib/loader.js):
    SyntaxError: missing ) after argument list

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael R., 2021-01-19
@Mike_Ro

Question 3: with this implementation, the connection of other templates (header and footer) to the current template falls off. What to do?

Download https://www.npmjs.com/package/ejs-webpack-loader .
webpack.config.js:
module: {
  rules: [
    {
      test: /\.ejs$/,
      use: [
        {
          loader: "ejs-webpack-loader",
          options: {
            data: {prop: true},
            htmlmin: true
          }
        }
      ]
    },
  ]
}

page_landing_1.ejs:
<%- include header.html -%>
<% if(prop) { %>
<h1><%= prop %></h1>
<% } %>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question