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