Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple views directories for one expressjs app

Tags:

express

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

like image 724
jascha Avatar asked Jan 21 '13 07:01

jascha


3 Answers

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', ...);
like image 73
Kwal Avatar answered Oct 11 '22 16:10

Kwal


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

like image 26
jascha Avatar answered Oct 11 '22 14:10

jascha


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.

like image 22
Kelly Orr Avatar answered Oct 11 '22 16:10

Kelly Orr