Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Templating library for node.js with php-like syntax

Is there a library for node.js to "parse" a content of a file with specific syntax? In example, I have a file which I want to serve on my node.js server:

<!DOCTYPE html>
<html>
<head>...</head>
<body>
    <?node echo Date.getTime(); ?> <!-- or something like this, I hope you have got the idea -->
</body>
</html>

Then it returns a HTML document:

<!DOCTYPE html>
<html>
<head>...</head>
<body>
    08.08.2013 <!-- or something like this, I hope you have got the idea -->
</body>
</html>

I don't know how to describe it more precisly, something like PHP for Apache server, but for node.js server.

like image 583
Are Avatar asked Dec 26 '22 22:12

Are


2 Answers

Just use CGI-Node. It allows you to run Node.js on any web hosting just like PHP:

<html>
    <head>
    </head>
    <body>
        <? var helloWorld = 'Hello World!'; ?>
        <?= helloWorld ?>
    <br>
    <b>I can count to 10: </b>
    <? for (var index = 0; index <= 10; index++) { ?>
        <?= index ?>
    <? } ?>
    </body>
</html>
like image 109
niutech Avatar answered Dec 28 '22 11:12

niutech


EJS templates look and feel like PHP and ASP but are pure JS: https://ejs.co/

Their example:

<ul>
<% for(var i=0; i<supplies.length; i++) {%>
   <li><%= supplies[i] %></li>
<% } %>
</ul>
like image 41
SheetJS Avatar answered Dec 28 '22 12:12

SheetJS