queckezz/koa-views

Multiple koa-views middlewares

pbalaga opened this issue · 4 comments

Is it possible to use multiple koa-views middleware objects in one application? Adding it once within app like below works.

app.use(views(__dirname + '/views', { map: {pug: 'pug'}, extension: 'pug' }));

However, when I add another instance of the middleware but pointing to another directory, it cannot find the views from the latter. In this example koa-views doesn't even look in /admin/views directory. It 'encouters' the first koa-views middleware, does not find a matching view in /views, and stops.

app.use(views(__dirname + '/admin/views', {
    map: {pug: 'pug'},
    extension: 'pug'
}));

How do I make koa-views continue searching for files if there are multiple middleware instances and the first one does not find the requested view?

Thanks

So basically you want to be able to specify multiple folders which koa-views is searching in? Seems like a reasonable addition.

Originally, the purpose was to find a way to configure view folders for different areas of the application separately. E.g. when configuring routes for 'admin', search for views in folder X. If routing to 'user', search in folder Y. Eventually, I found a way to isolate koa-views with koa-router. This is probably a better way to go and it solved the original goal in a different way.

That being said, being able to provide multiple view folders would be useful. Someone may want to search in a primary folder first and then fall back to a shared folder (containing common views).

Originally, the purpose was to find a way to configure view folders for different areas of the application separately. E.g. when configuring routes for 'admin', search for views in folder X. If routing to 'user', search in folder Y. Eventually, I found a way to isolate koa-views with koa-router. This is probably a better way to go and it solved the original goal in a different way.

Cool! I think that's the way to go. If you want, you can share some of the code so we can provide an example for others to see. ✨

That being said, being able to provide multiple view folders would be useful. Someone may want to search in a primary folder first and then fall back to a shared folder (containing common views).

I'll happily accept a PR.

Hey @pbalaga, can you share your solution? Im trying to figure out how to get koa-views to import two different folders.

Thanks.