EJS Flashcards
What is EJS?
Templating language that lets you generate HTML markup with plain JavaScript
Useful when you want to use a single HTML page, but with different contents/data based on conditions
Creating your first EJS Template
npm install ejs In .js, app.set(‘view engine’, ‘ejs’); Create a new folder called ‘views’ Add a file with .ejs .ejs file follows HTML format, but has ejs variable placeholders Placeholder marker
To display a ejs webpage,
res.render(>);
Running Code Inside the EJS Template
We can add JavaScript code in the EJS file.
Each line of the JavaScript needs to have an EJS scriptlet of
Use scriptlet only for control-flow, but only in select situations.
Most logic needs to reside in the server code
Passing Data from Your Webpage to Your Server
- This will be done via a POST request from the web page.
- Server, let’s say app.js, will then handle the POST request.
- It can use body-parser, to then obtain the data being sent, and work with the data
res. redirect()
The Concept of Scope in the Context of Javascript
Variables inside of a function, have local scope to that function
Variables outside of a function, have global scope
If/Else or For/While
Variable created will have global scope
let will have local scope
const will have local scope
Use let or const whenever you can, over var
Adding Pre-Made CSS Stylesheets to your Website
All files that will be used must be served up in express app.js, these are static files
Static files are files that clients download as they are from the server.
Understanding Templating vs Layouts
Templating
Used mainly when you want to reuse the structure and format of a web page, but changing the data within them
Layouts
When you want to reuse certain parts of a webpage, like the headers, footers, and styling, in many different webpages
But overall, the body structure differs
Use for reusing parts of the webpage
Reused component pages are called partials
Understanding Node Module Exports: How to Pass Functions and Data Between Files
Using functions between files:
Node.js contains an object called module
If you want to create functions in a class that you want it to be reusable in other files:
You can set module.exports. =
module.exports can also be shortened down, and exports can be used
To use the function:
Create a reference variable, and link it to the file where the reusable functions reside
Be sure to use __dirname + if it’s custom, because we didn’t install using npm
Then invoke the function using .
Blog Website Challenge
Usually, the npm packages that are being used are gitignored, and when a new person works with the project they would then initially go ahead and run npm install
Express Parameter Routing
Lodash