Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: define ETA engine in Express walkaround #67

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 57 additions & 16 deletions docs/resources/express.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,69 @@ Eta no longer supports the Express.js `app.engine()` function, but it's still co

```js
const express = require("express")
const app = express()
const path = require("path")
const port = 3000
const path = require("node:path")
const { Eta } = require("eta")

var { Eta } = require("eta")
const app = express()

// views directory
let viewpath = path.join(__dirname, "views")
// on Deno, use let viewpath = Deno.cwd()+'/views/'
let eta = new Eta({ views: viewpath, cache: true })
const eta = new Eta({
// Views directory path
views: path.join(__dirname, "views"), // on Deno : `${Deno.cwd()}/views/`

/* no more needed with Deno or Node (Eta v3.x.x)
app.engine("eta", eta.render)
app.set("view engine", "eta")
*/
// Any other option...
cache: true
})

app.get("/", (req, res) => {
// use status(statusNumber) and send(template)
res.status(200).send(eta.render("index", { title: "Hey", place: "Hello there!" }))
const renderedTemplate = eta.render("index", { title: "Hello", place: "there!" }) // create `index.eta` in the `views` folder
res.status(200).send(renderedTemplate)
})

app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
app.listen(3000, () => {
console.log("Server listening on port 3000")
})
```

### Alternatively, define an Express app engine using ETA

Missing the good'old `res.render` to render computed templates ? Here is a quick walk around to achieve the same behaviour throughout the whole application.

Note : `app.engine("eta", eta.render)` is no longer supported on `v3.x.x` for Node.js and Deno


```js
const express = require("express")
const path = require("node:path")
const { Eta } = require("eta")

// Create app
const app = express()

// Setup eta
const eta = new Eta({ views: path.join(__dirname, "views") })
app.engine("eta", buildEtaEngine())
app.set("view engine", "eta")

// Home route
app.get("/", (req, res) => {
res.render("home", { message: "Hello world !" }) // create `home.eta` in the `views` folder
});

// Start server
app.listen(3000, () => {
console.log("Server listening on port 3000")
});


function buildEtaEngine() {
return (path, opts, callback) => {
try {
const fileContent = eta.readFile(path);
const renderedTemplate = eta.renderString(fileContent, opts);
callback(null, renderedTemplate);
} catch (error) {
callback(error);
};
};
}
```