Jascha Ehrenreich
Jascha Ehrenreich

Reputation: 557

multiple views directories for one expressjs app

i basically have an expressjs application with the following dirs:

app/views

and

app/plugin/views

and would like to be able to serve from both directories.

how will this work?

i tried setting

app.set('views', second_dir)

but then my app wont find my first view directory.

then i tried symlinking the folder,

fs.exists(viewDir, function() {
    fs.symlink(viewDir, rootViewDir, 'dir', function(err) {
        console.log('adding symlink with err: '+err+ ' in viewdir ='+viewDir+' rootDir ='+rootViewDir);
    });
  });

this works, creates the symlink (permanently), but node is not able to read symlinked views as it seems.

is there a solution for my dilemma?

thanks a lot, have fun jascha

Upvotes: 8

Views: 2791

Answers (3)

Kelly Orr
Kelly Orr

Reputation: 751

I am running Node.JS v0.8.20, Express.JS v4.2.0 and EJS v0.8.8 I have my 'views' path defined in app.js as:

app.set('views', path.join(__dirname, 'views'));

I have defined by views folder structure to be \views\{model}\{operations}. For example: \views\products\add.ejs.

When I reference the view in the route file such as in \routes\products.js, I reference the subdirectory and escape the backslash. For example, to reference the views\products\add.ejs file, I use:

res.render('products\\add');

I like this format in that it allows me to keep my model operations in views one per file and then group the operations per model object in folders.

Upvotes: 1

Jascha Ehrenreich
Jascha Ehrenreich

Reputation: 557

found this video by tj holowaychuk where he just creates additional express applications for the plugins. http://vimeo.com/56166857

he also specially mentions inheritance of "global" variables in the comments of the video, which means that the app.locals and stuff like that should be the same in every express app in the chain of apps and plugins, this will be put to the test now :)

anyways, just wanted to answer my own question with an answer i originally dismissed without testing (because i didnt assume that this global linkage of variables exists ;) )

have fun

jascha

Upvotes: 2

Kwal
Kwal

Reputation: 1531

Why not change how you've got your views structured so that the 'plugin' folder exists underneath your 'views' folder? Then when you render your views, you can specify the sub-path at that time:

res.render('plugin/pluginview1', ...);

Upvotes: 3

Related Questions