Express under Ejs basics of Getting Started

Source: Internet
Author: User

Using Ejs in 1.express

var express = require (' Express ');//need to install Express

var app = Express ();

App.set ("View Engine", "Ejs");//template engine set to Ejs

2. Document organization

Using Ejs in Express, the file organization follows Express.

. views-------placing a dynamic template

. public------Place Static Web pages

. layouts-----Placing a layout file

3. Basic syntax

. <% Code%>

Non-buffered Conditional statement element

. <%= Code%>

Escape HTML, the code and print it out

. <%-Code%>

Non-escaped buffering, which is code and will print out

. <% include file%>

Embed other files

. <% layout (file)-%>

Specifying a layout file

. <% script (file)-%>

Include JS script file

. <% stylesheet (file)-%>

Include CSS files

. <% block (code, code)-%>

Specifying block contents

4. Basic objects

. scripts

The included script

. stylesheets

The included CSS

. Blocks

The contained blocks

5. Here is an example of a combination of the above usages

Main Page:

index.ejs<% layout (' boilerplate ')-%><% script (' foo.js ')-%><% stylesheet (' foo.css ')-%>

Layout file:

boilerplate.ejs<! DOCTYPE html>

App.js in config file:

App.jsvar Express = require (' Express ')  , engine = require (' ejs-locals ')  , app = Express ();//Use Ejs-locals for All Ejs templates:app.engine (' Ejs ', engine); App.set (' views ', __dirname + '/views '); App.set (' View engine ', ' Ejs '); So can render (' index ')//render ' index ' into ' boilerplate ': app.get ('/', function (req,res,next) {  Res.render (' Index ', {what: ' best ', who: ' Me ');}); App.listen (3000);

Final execution Result:

<! DOCTYPE html>

The article knowledge from: Here, small North slightly changed to change hehe, change not big.

Express under Ejs basics of Getting Started

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.