EJS Basics - JamesDansie/data-structures-and-algorithms GitHub Wiki
EJS Intro
Author: James Dansie
EJS is language for extending html with a bit of JavaScript. It allows you call variables from your JS files, but also allows for some basic JS functions such as loops and if statements. Remember to install ejs using npm install --save ejs express body-parser cors. This will install all the necessary dependencies. To get started you'll need to include the following in your server.js file;
- let bodyParser = require('body-parser');
- let expressLayours = require('express.ejs.layouts');
- app.use(expressLayouts);
- app.use(bodyParser());
if you're using a views folder then include;
- let path = require('path');
- app.set('views', path.join(__dirname, 'views'));
- app.set('view engine', 'ejs');
Instead of ending your html files in .html, now use .ejs. To call ejs in the file use <%- VARIABLES %> to call other files or variables. To call ejs commands in the .ejs file us <% {CODE GOES HERE} %>
References