简体   繁体   中英

how to set and use partials view paths in node.js + expressJS

I am having problems in setting partial view paths in nodeJs + expressJs. Below is my directory structure.

Directory Structure

packages
--Module1  
----Views
------sample.ejs
--sharedModule
----Views
------partials
--------Module1.ejs
--------partialHTML.ejs      //able to use in index.ejs
------index.ejs  //Used : <% include partials/partialHTML%>

ExpressJS:

var express = require('express');
var app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'packages/sharedModule/views'));

I am able to use 'partialHTML.ejs' in index.ejs file using <% include partials/partialHTML%>. .

How can i use Module1.ejs files under Module1/view/sample.ejs file ?

You've set packages/sharedModule/views as your views directory, sample.ejs is no where under that dir, it's under another path. That's why it's not found.

In Linux you could create a soft link to point the shared views Module1/views/sharedviews ==> ../../sharedModule/views , then set your Module1 as the root path for views -

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

So when you look for any view, it's guaranteed to be found, since now your views dir has both Module1 and sharedModule views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM