Change the default views folder in adonis


#1

Hi guys, how are you doing?

I’m looking foward to build a huge application using adonis, and one of many questions i have, is how can i set a theming strucure using adonis. Eg. I have a theme struture similar to wordpress and i want to set it dynamically which theme would be used by adonis when the app is loaded. however, without setting that hardcoded in my project controllers.

Eg.
My theme structure is as following:

|- resources
   |- views
     |- default (theme)
     |   | -  posts
     |       |- index.edge
     |- vaporwave (theme)
     |   | -  posts
     |       |- index.edge
     |- future (theme)
     |   | -  posts
     |       |- index.edge

SampleController.js

...
const posts = await Post.query().paginate(20)
return view.render('posts.index', posts)
...

So i want adonis render the correct posts.index.edge view, According with a setting defined on my database or .env file.

Anyone realizes how can i do this?


#2

Since you’re not actually changing the base path, but instead putting things into subdirectories within the base view path, you could easily throw a variable value onto the front end of any/all views your controllers return.

Something like this:

return view.render(`${theme}.posts.index`, posts)

More likely the theme would be determined in some middleware and attached to the request object, or defined as part of the user object and therefore available as part of the auth object… so that you don’t have to repeat the logic to lookup the theme everywhere… in that case you’d probably do something like this:

return view.render(`${auth.theme}.posts.index`, posts)

In any case, this would be the easiest approach given the directory structure you propose.


#3

Not all hero use capes hehehehe thx man!