EJS Flashcards

1
Q

Get started with embedded javascript

A

npm install ejs

after express is required in javascript, we write:

app.use(‘view engine’ , ‘ejs’);

make a folder in your root called ‘views’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Where do you put your ejs file?

A

We made a folder called ‘views’ - that’s where you put the index.ejs file - which is basically an HTML files with ejs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

.render( )

A

res.render(‘the-ejs-file’ , { the variable to pass to ejs file } );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

res.locals

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

EJS HTML

A

https://ejs.co/

Visit this page to learn more about the codes for this type of HTML

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

TAGS for EJS

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

if-else in EJS

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

(angl)%- include(“ejs-file-in-views-folder”) -%(angl_clos)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly