RandomEtc / ejs-locals

Express 3.x layout, partial and block template functions for the EJS template engine.
298 stars 63 forks source link

Load files (partials, ...) using absolute path #45

Closed Vadorequest closed 9 years ago

Vadorequest commented 9 years ago

Load files using absolute path

I've added a way to load files (I tried with partials only) using an absolute path because the relative fails when loading files loaded from a file that has been itself loaded through include/partial functions. (somehow the path happens to be wrong, sometimes correct depending on which file I save in last and some other weird stuff)

This is a feature that has been asked many times.

Examples

1. Absolute with global config

It is possible to set a __config global object that would look like this:

__config = {
  path: {
    base: __dirname
  }
};

partial('test/fixtures/partials/box.ejs', {_useAbsolute: true, box_title: 'test'})

Where __config.path.base would contain the basepath of the application, or the basepath from where you want to load files, but would be used only when using _useAbsolute: true.

2. Absolute with local config

// Use the absolute path provided by _basePath
partial('test/fixtures/partials/box.ejs', {_basePath: __dirname, box_title: 'test'})

Here you set the _basePath when calling the function, I don't like it because I want to use always the same config, or at least most of the time without having to set it each time (see 1. Absolute with global config)

But I believe it can be useful from time to time.

3. Use relative path (old way, still compatible)

// use the relative path from the current directory.
partial('test/fixtures/partials/box.ejs', {box_title: 'test'})

Design

I decided to use the _ before the keys I've created to obviously avoid issues. I believe the use of _ shows the key is somehow private to the function for its own use. I was afraid to mess up existing app by using a common/usual key.

Test

As I said, I didn't write proper test, I made my tests directly in debug mode. But I have installed my ejs-locals on another app to test it and it worked fine there too.

Vadorequest commented 9 years ago

I believe this cannot be merged as it yet, but I also believe you'll tell me what to change here, I don't like the global __config stuff, it was just a way to make it work quickly based on my own issue. I'm not sure what would be the best way to handle it though.

Vadorequest commented 9 years ago

I cancel the PR, anyway it's never gonna get merged. I better to make my own implementation of ejs-locals

I've made too much changes anyway. https://github.com/Vadorequest/ejs-locals/tree/2.0.0