简体   繁体   English

使用类似PHP的语法为node.js模板库

[英]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? 是否存在供node.js使用的库,以使用特定语法“解析”文件的内容? In example, I have a file which I want to serve on my node.js server: 例如,我有一个文件要在我的node.js服务器上提供:

<!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: 然后它返回一个HTML文档:

<!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. 我不知道如何更精确地描述它,比如用于Apache服务器的PHP,但是用于node.js服务器。

EJS templates look and feel like PHP and ASP but are pure JS: https://ejs.co/ EJS模板的外观和感觉类似于PHP和ASP,但都是纯JS: https : //ejs.co/

Their example: 他们的例子:

<ul>
<% for(var i=0; i<supplies.length; i++) {%>
   <li><%= supplies[i] %></li>
<% } %>
</ul>

You are talking about a template engine. 你在谈论模板引擎。 There are many possibilites, one of the most popular is jade : 有许多可能性,其中最受欢迎的是

http://jade-lang.com/ http://jade-lang.com/

It is especially good when integrated with Express frawework . Express frawework集成时,它特别好。 You can find a big list of template engines here: 您可以在此处找到大量模板引擎:

https://github.com/joyent/node/wiki/modules#wiki-templating https://github.com/joyent/node/wiki/modules#wiki-templating

You can use Underscore templates . 您可以使用Underscore模板 It lets you write templates like this: 它使您可以编写如下模板:

<ul>
<% _.each(people, function(name) { %>
  <li><%= name %></li>
<% }); %>
</ul>

Just use CGI-Node . 只需使用CGI-Node It allows you to run Node.js on any web hosting just like PHP: 它允许您像PHP一样在任何虚拟主机上运行Node.js:

<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>

I might be a little late to the party, but I worked on something like this yesterday and it works surprisingly similar to php. 我可能会晚一点,但我昨天做了类似这样的事情,它的工作方式与php类似。 Eg you can do stuff like 你可以做类似的事情

<?j
include_once("header.jhtml");

for(var i = 0; i < 10; i++) { ?>
<span id="<?j print(i) ?>">

<?j}
print($.req.url);
include("footer.jhtml");
?>

which will include the header file (if it was not included before) just like in php and then print 10 spans with id's from 0 to 9, then print the request url ($ is the context variable which includes the request data), then include the footer file. 这将包括头文件(如果它之前没有包含),就像在php中然后打印10个跨度,id为0到9,然后打印请求url($是包含请求数据的上下文变量),然后包括页脚文件。 So it's basically php with js syntax. 因此,基本上是具有js语法的php。 I will make it available on npm probably this weekend. 我可能会在本周末的npm上提供它。 It's very much in it's early stage though, as I said, I worked 1 day on it. 但是,正如我所说的那样,它还处于初期阶段,所以我只花了一天的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM