ejs if else语法
Hi! Welcome to NODE AND EJS TEMPLATE ENGINE SERIES. Today, we will talk about EJS conditions, that is the if/else statement.
嗨! 欢迎使用NODE和EJS模板引擎系列 。 今天,我们将讨论EJS条件 ,即if / else语句 。
EJS gives us the flexibility to fill it's template depending on a particular condition.
EJS使我们可以根据特定条件灵活地填充其模板。
FOR EXAMPLE - Let's assume you have a template for a social media of 12+ (age) users only.
例如-假设您只有12个以上(年龄)用户的社交媒体模板。
You can control the number of people signing up by placing a condition as seen below,
您可以通过设置以下条件来控制注册人数,
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) { // root route or home route
res.render("home", {
age: 16
});
});
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
<% if (age > 12) { %>
<p> valid!!!</p>
<%} else { %>
<p> Not Allowed </p>
<% } %>
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
In this example, our condition declares if age variable is greater than 12, let the phrase valid be displayed else, Not allowed phrase will be displayed..
在此示例中,我们的条件声明age变量是否大于12,则使短语有效显示,否则,将显示不允许的短语。
Take note of the tags I used in my ejs code.
记下我在ejs代码中使用的标签。
In this case, age is assigned to 12.
在这种情况下,年龄分配为12。
You can test your own values.
您可以测试自己的值。
Output
输出量
Thanks for coding with me! Feel free to drop a comment or question.
感谢您与我编码! 随意发表评论或问题。
翻译自: https://www.includehelp.com/node-js/ejs-if-else-statement-ejs-conditions.aspx
ejs if else语法