initial
This commit is contained in:
commit
74ae93a96e
1 changed files with 94691 additions and 0 deletions
61
joplin notion extension/Web Development/nodejs/ejs.md
Normal file
61
joplin notion extension/Web Development/nodejs/ejs.md
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
ejs
|
||||
|
||||
https://www.digitalocean.com/community/tutorials/how-to-use-ejs-to-template-your-node-application
|
||||

|
||||
|
||||
layouts
|
||||
use this for layouts https://www.npmjs.com/package/express-ejs-layouts
|
||||
|
||||
```js
|
||||
const express = require('express');
|
||||
const expressLayouts = require('express-ejs-layouts');
|
||||
const app = express();
|
||||
|
||||
// Set EJS as the view engine
|
||||
app.set('view engine', 'ejs');
|
||||
|
||||
// Use the layouts middleware
|
||||
app.use(expressLayouts);
|
||||
|
||||
// Optional: Define a default layout file
|
||||
app.set('layout', 'layouts/layout');
|
||||
|
||||
```
|
||||
|
||||
layout at `views/layouts/layout.ejs`
|
||||
```html
|
||||
...
|
||||
<body>
|
||||
<main>
|
||||
<!-- Individual page content is injected here -->
|
||||
<%- body %>
|
||||
</main>
|
||||
</body>
|
||||
|
||||
|
||||
```
|
||||
|
||||
view at `views/index.ejs`
|
||||
```html
|
||||
<h1>Welcome to my Homepage!</h1>
|
||||
<p>This is the main content of the home page.</p>
|
||||
|
||||
```
|
||||
|
||||
for custom layout
|
||||
```js
|
||||
app.get('/admin', (req, res) => {
|
||||
res.render('admin-dashboard', { layout: 'layouts/admin-layout' });
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
when using variable which might be undefined this is safest way to do it
|
||||
|
||||
```html
|
||||
<% if( typeof keyword!=='undefined' && keyword ){ %>
|
||||
<br>
|
||||
<% } %>
|
||||
```
|
||||
Loading…
Reference in a new issue