ejs for循环中的判断_EJS for循环

ejs for循环中的判断

Hi! Welcome to NODE AND EJS TEMPLATE ENGINE SERIES. Today, we will talk about EJS loops, precisely the for loop.

嗨! 欢迎使用NODE和EJS模板引擎系列 。 今天,我们将讨论EJS循环,确切地说是for循环

The for loop is very useful and used in several special web applications.

for循环非常有用,并在几个特殊的Web应用程序中使用。

Take Note! You should have Node.js installed in your before you can start using EJS in this article.

做记录! 在开始使用EJS之前,您应该已经安装了Node.js。

To download Node JS, visit nodejs.org, then install.

要下载Node JS,请访问nodejs.org ,然后安装。

* BASIC NODE.JS/EXPRESS KNOWLEDGE REQUIRED

*需要基本NODE.JS /表达知识

To begin, ensure you have EJS and express installed via npm.

首先,请确保您已通过npm安装了EJS和express。

To install EJS, simply open a terminal/command prompt and type the following command:

要安装EJS,只需打开终端/命令提示符并键入以下命令:

    npm install ejs
        or
    npm install ejs –save

You can use either command prompt or PowerShell as terminal.

您可以使用命令提示符或PowerShell作为终端。

We will create 2 files as usual, one for our express server file and the second our ejs file.

我们将照常创建2个文件,其中一个用于我们的快速服务器文件,另一个用于我们的ejs文件。

EJS helps us embed JavaScript code, if statements and loops inside html.

如果html中的语句和循环,EJS可以帮助我们嵌入JavaScript代码。

Open your text editor and type the following code, save as app.js.

打开文本编辑器,然后输入以下代码, 另存为app.js。

var express = require('express');
var ejs = require('ejs');
var app = express();
app.set('view engine', 'ejs');

app.get("/", function(req, res) {
    res.render("home");
});
app.listen(3000, function() {
    console.log("server is listening!!!");
});

Unlike any other express app, we used res.render then the ejs file and not res.send.

与任何其他Express应用程序不同,我们使用res.render然后使用ejs文件,而不使用res.send

Also, I didn't require the ejs app. There's no problem!. You can do so if you wish.

另外,我不需要ejs应用。 这里没有问题!。 如果您愿意,可以这样做。

Now, let's create our ejs files:

现在,让我们创建我们的ejs文件:

Open a text editor and type the following code, save as home.ejs

打开文本编辑器,然后输入以下代码,另存为home.ejs

<% for (var i =1; i <=10;  i++ ) { %>
<br/>  <%= i %> 
<%#  will output the numbers 1-10 %>
<% } %>

Your ejs file should be saved in the views folder found in your node project directory.

您的ejs文件应保存在节点项目目录下的views文件夹中。

Take Note, the folder name views is not a random word I selected but it's the reserved folder name where express checks for template engine by default.

请注意,文件夹名称视图不是我选择的随机词,而是默认情况下Express检查模板引擎的保留文件夹名称。

Finally, initiate the app.js file with node app.js in a terminal and view the port in a browser.
localhost:3000

最后,在终端中使用节点app.js初始化app.js文件,并在浏览器中查看端口。
本地主机:3000

  • The code above will output an increment of 1-10.

    上面的代码将输出1-10的增量。

  • You can see the different type of tags used in each case.

    您可以看到在每种情况下使用的不同类型的标签。

  • You can equally see how comments are written in EJS using the tag:

    您同样可以使用标记查看如何在EJS中编写注释:

        <%#  comments here! %>
    
    

Output

输出量

for loops in EJS

Thanks for coding with me! Feel free to drop a comment or question.

感谢您与我编码! 随意发表评论或问题。

翻译自: https://www.includehelp.com/node-js/ejs-for-loops.aspx

ejs for循环中的判断

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值