I want nodejs to process templates like erb

Asked 2 years ago, Updated 2 years ago, 159 views

ruby uses erb as the default for template processing.

When nodejs wants to do the same template processing, what libraries are common to do this?(Is there such a thing?)

node.js

2022-09-30 21:33

3 Answers

Node.js has something called EJS.
http://www.embeddedjs.com/

You can install it with the npm command.
https://www.npmjs.com/package/ejs


2022-09-30 21:33

In terms of template (layout) extensions, Nunjucks is easy.
(I think EJS found it troublesome to expand the template.)

You can extend the template as follows:

layout.html

<body>
  <header class="main">header</header>
  <section class="content">
    <nav class="side_nave">navi</nav>
    <main>
       {% US>block main%}
       dummy
       {% endblock%}
    </main>
  </section>
</body>

index.html

{%extends"layout.html"%}
{% US>block main%}
This is the main contents
{% endblock%}

index.html Render Results

<body>
  <header class="main">header</header>
  <section class="content">
    <nav class="side_nave">navi</nav>
    <main>
       This is the main contents
    </main>
  </section>
</body>


2022-09-30 21:33

In nodejs, pug, handlebars are relatively famous, but there are many other factors, so you can choose according to your preference.The syntax close to erb seems to be EJS.

You can also use the recently rising popularity framework Vue.js as a template to create server-side applications with Nxt.js or static sites with VuePress.If you like new things, it will be interesting to use them.


2022-09-30 21:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.