trailsjs / trailpack-hapi

:package: Hapi.js Trailpack

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proper way to set up views?

thejones opened this issue · comments

I added the info per the read me and set the engine in the views.js file and added inert and vision in web.js.

Inside web.js I also added

views: {
    engines: {
      html: require('handlebars')
    },
    path: 'views'
},

In ViewController (default from yeoman) I changed the HelloWorld to:

  helloWorld (request, reply) {
    reply.view('about');
  }

I believe this is correct but could be wrong. I created the views folder and added about.html inside. This folder is directly under the project:

project
-api
-config
-views
-test
-node_modules

This gives a 500 error and the console gives

error: View file not found: `about.html`. Locations searched: [/Users/ajones/git/training/trails_testing/config../views/about.html]

So it appears that it does not lookup the path correctly.

I tried setting the path in various ways but same message.

path: '../views'

As well as using relativeTo: http://hapijs.com/tutorials/views

relativeTo: __dirname,
path: 'views'

I am not sure what is missing but thought I would provide this as maybe somebody else will need the info as well.

In my project if I comment out https://github.com/trailsjs/trailpack-hapi/blob/master/config/web.js#L17 it fixes the above issue. So, I assume it is just not getting passed in from my project correctly.

this is happening on mine also even if i comment that out or not. Any help on this would be nice, or just a response as if it is a bug or a config issue so we wont waste too much time on this framework.

@grd2345 commenting out the path inside the trailpack-hapi works for me. This is my web.js inside my project:

module.exports = {

  /**
   * The port to bind the web server to
   */
  port: process.env.PORT || 3000,
    views: {
    engines: {
      html: require('handlebars')
    },
    path: '../views'
    },
  plugins: [
    {
      register: require('vision'),
      options: { }
    },
    {
      register: require('inert'),
      options: { }
    }
  ]
}

Also, make sure that you have installed Inert and Vision plugins as they are required for Hapi. That being said Trails is in alpha status so keep that in mind during your evaluation. Roadmap for the planned releases can be found here. https://github.com/trailsjs/trails/blob/master/ROADMAP.md

I believe there is a bug or was it intended that we change the config/main.js paths.root to what we need. Currently by default it is setting this property to __dirname + '../', which is causing the view engine to look for the views in a wrong path ( config/../views). if i change this property to the following. /home/jerry/repos/riverchurch.org, which is actually my root directory it now finds the views in the correct location. So it seems this issue is actually this property in the config/main.js file, just a heads up on the contributors to this issue.

PS, I was never able to get jade to work, only handlebars so far.

__dirname + '../'

Probably I should instead set the default to something like path.resolve(__dirname, '..'). Can you try that and see if the path works correctly?

this worked well. also i found the issue with why jade doesnt render
the following code only is looking for html engines which it also has to look for jade.
I modified it as a test to let an engine as jade pass through and it works as a charm with jade.

registerViews (app, server) {
    if (!app.config.web.views.engines.html) {
      app.log.debug('config.views: No view engine is set. Not loading.')
      return
    }

    server.views(app.config.web.views)
  }

@tjwebb I ran into this too and the fix you described worked for me. I want to open a PR but it's not clear which repo contains config/main.js. Can you point me in the right direction?

@wbprice there is an open-ish PR that has this fix included. https://github.com/trailsjs/trailpack-hapi/pull/37/files

It is not complete as I had ran into some issues.

@tjwebb

Is there further work to be done on this issue? We have updated the way views are registered since this issue was filed.