天天看點

ejs if else文法_EJS If Else語句(EJS條件)

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 輸出量
ejs if else文法_EJS If Else語句(EJS條件)

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