EJS Flashcards
Get started with embedded javascript
npm install ejs
after express is required in javascript, we write:
app.use(‘view engine’ , ‘ejs’);
make a folder in your root called ‘views’
Where do you put your ejs file?
We made a folder called ‘views’ - that’s where you put the index.ejs file - which is basically an HTML files with ejs.
.render( )
res.render(‘the-ejs-file’ , { the variable to pass to ejs file } );
res.locals
res.render( ) can only be passed ONCE. So if you are looking to send out multiple variables to ejs file, we use res.locals
res. locals.variablename = variable to store;
res. render ( );
to send the variables stored in res.locals, just pass res.render at the end without any arguments inside the parenthesis.
EJS HTML
https://ejs.co/
Visit this page to learn more about the codes for this type of HTML
TAGS for EJS
There are more tags used in EJS. But these will be very common ones.
….cant write the syntax here.
Head over to the documentation to see them
(angl) % %(angl_clos) ————–> control flow statement
(angl) %= %(angl_clos) ————> for variable
(angl) %- include( ) -%(angl_clos) ———-> include used in template
(angl) %# %(angl_clos) ———-> comment
if-else in EJS
This is a weird one and if not written exactly - they will not produce the expected output.
Cant write the syntax here either…here’s a hack –
(angl)% if (conditonal) { %(angl_clos) // statement here (angl)% } else { %(angl_clos) // statement here (angl)% } %(angl_clos)
(angl)%- include(“ejs-file-in-views-folder”) -%(angl_clos)
This aids in further templating so we can have static header and footer whereever this command is declared.
This is position specific, so we need to be careful where we are writing this.