mde / ejs

Embedded JavaScript templates -- http://ejs.co
Apache License 2.0
7.7k stars 841 forks source link

Add documentation to explain how "include" resolves file paths #675

Closed littlebenlittle closed 2 years ago

littlebenlittle commented 2 years ago

I cannot get include to work in EJS templates using this script:

build.js

const fs = require('fs')
const ejs = require('ejs')
const process = require('process')

const spacer = (width) => {
    return '<div class="w3-container w3-content w3-col l' + width + '" aria-hidde="true"></div>'
}

fs.readFile("src/index.ejs", "utf-8", (err, data) => {
    if (err) {
        console.log(err)
        process.exit(1)
    }
    let html = ejs.render(data, {spacer})
    process.stdout.write(html)
})

Directory structure

build.js
src
├── index.ejs
└── partials
    └── intro.ejs

index.ejs

...
    <%- include('src/partials/intro.ejs') %>
...

Running this with node build.sh results in Could not find the include file "src/partials/intro.ejs". I have tried using "./partials/intro.ejs" too.

It would be very helpful to understand how EJS is trying to resolve paths.

mde commented 2 years ago

The relevant code for include resolution is here: https://github.com/mde/ejs/blob/main/lib/ejs.js#L156

But I think the root and views options documented in the README and on the Web site will give you what you need.