appweb ejs_EJS布局

news/2024/7/10 0:29:21 标签: nodejs, vue, python, npm, js

appweb ejs

Hi! Welcome. Today, we are going to look at EJS layouts. EJS Layouts make very important use of EJS. Have you ever tried to imagine if social media websites create a new webpage for every user?

嗨! 欢迎。 今天,我们将看一下EJS布局EJS布局非常重要地使用了EJS 。 您是否曾经想过,社交媒体网站是否会为每个用户创建一个新网页?

Some simply make use of layouts in template engines.

有些只是在模板引擎中使用布局

We are going to see an example or possible use of layouts using express and the express-ejs-layouts module.

我们将看到一个示例或使用express和express-ejs-layouts模块的布局的可能用法。

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 js>nodejs.org, then install.

要下载Node JS,请访问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

Equally, install the express-ejs-layouts module using the command npm install express-ejs-layouts.

同样, 安装使用命令NPM安装快递-EJS的布局 快递-EJS-布局模块

We will create,

我们将创造,

  1. Express server file

    Express服务器文件

  2. Home.ejs file

    Home.ejs文件

  3. About.ejs file

    About.ejs文件

  4. Layout.ejs file and all ejs file should be saved in the views folder found in your node.js project directory

    Layout.ejs文件和所有ejs文件应保存在node.js项目目录下的views文件夹中

Layouts enable us to dynamically fix content or elements to a page in such a way that even if a different page is requested, the content remains but the page is successfully accessed.

布局使我们能够以某种方式动态地将内容或元素固定到页面,即使请求另一个页面,该内容或内容仍会保留,但该页面可以成功访问。

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();
var expressLayouts = require('express-ejs-layouts');

app.use(expressLayouts);
app.set('view engine', 'ejs');

app.get("/", function(req, res) {
    res.render("home");
});

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

Now, let's create our ejs files:

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

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

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

<h4> Home Page</h4>

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

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

<h5> hi!... I am born again and have been set free!! </h5>

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

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

<html>

<head>
    <title> EJS</title>
</head>

<body>
    <div style="background-color:yellow; padding:50;">
        <h2> <center> EJS IS COOL!!</h2>
    </div>
    <%- body %>
</body>

</html>

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

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

    localhost:3000 and localhost:3000/about

Output:

输出:

EJS Layouts 1
EJS Layouts 2

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

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

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

appweb ejs


http://www.niftyadmin.cn/n/1253770.html

相关文章

json对象加json对象_JSON对象

json对象加json对象We have heard time and again that everything in JavaScript is an object. 我们一次又一次地听到JavaScript中的所有东西都是对象。 This very fact was extensively used to weave the lightweight super popular data format called JSON. In JSON, al…

Codeforces 1110C (思维+数论)

题面 传送门 分析 这种数据范围比较大的题最好的方法是先暴力打表找规律 通过打表&#xff0c;可以发现规律如下&#xff1a; 定义\(x2^{log_2a1}\) &#xff08;注意&#xff0c;cf官方题解这里写错了&#xff0c;官方题解中定义\(x2^{log_2a}\)是有问题的 (1) 若\(a \neq 2^x…

CF1110D Jongmah

题意 给出\(n\)个在\(1\)到\(m\)范围内的数\(a_i\)&#xff0c;定义一个三元组为\((x,x,x)\)或者\((x,x1,x2)\)的形式&#xff0c;求最多能组成三元组的数量是多少 题解 比赛的时候脑抽了……也可能是过年之后就傻了…… 先看看贪心行不行&#xff0c;把所有能选\(3\)个的都先选…

SpringMVC整合Junit4进行单元测试

使用步骤: 1.在src/test/java下建立一个测试包2.创建一个测试基类3.把要测试的类拷到该包下,并继承基类注意&#xff1a;编写测试类的原则&#xff1a; ①测试方法上必须使用Test进行修饰②测试方法必须使用public void 进行修饰&#xff0c;不能带任何的参数③新建一个源代码…

java awt画布_Java AWT画布

java awt画布Canvas is a GUI component that creates a rectangular box on the screen. It can be used to draw shapes or print text, it acts as a canvas. It can also be used to take user inputs. It is called so because it is like a canvas on which artists draw…

Scala中的HashSet

Scala HashSet (Scala HashSet) A HashSet is a special type of collection that is unordered i.e. the order of inserted elements is not defined. For storing its elements and keeping track of them it uses hashmaps. HashSet是一种无序的特殊集合类型&#xff0c;即…

设计模式(十)——模板方法模式

AbstractClass&#xff08;抽象类&#xff09;&#xff1a;在抽象类中定义了一系列基本操作(PrimitiveOperations)&#xff0c;这些基本操作可以是具体的&#xff0c;也可以是抽象的&#xff0c; 每一个基本操作对应算法的一个步骤&#xff0c;在其子类中可以重定义或实现这些步…

ruby 生成哈希值_Ruby中的哈希

ruby 生成哈希值Ruby Hash集合 (Ruby Hash Collection) In Ruby, hashes are the collection of identical keys and their values. They are like dictionaries, which has an element along with its description. You can retrieve any available value with the help of a …