Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: View configuration to autoload helepers #846

Closed
Iheartweb opened this issue May 11, 2013 · 3 comments
Closed

Request: View configuration to autoload helepers #846

Iheartweb opened this issue May 11, 2013 · 3 comments
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@Iheartweb
Copy link

Helpers like partials are a nearly ubiquitous concept among tempting engines. Just like partials it would be nice to be able to express a path in the view config:

views: {
  engines: { html: 'handlebars' },
  path: __dirname + '/templates',
  partialsPath: __dirname + '/templates/withPartials',
  helpersPath: __dirname + '/templates/helpers'
} 

Currently I achieve the desired functionality by:

var Hapi = require('hapi'),
  handlebars = require('handlebars'),
  loader = require('node-glob-loader');

// Load View Helpers
loader.load('./application/views/helpers/*.js', function (exports) {
  exports(handlebars);
});

With the config: views.engine.module = handelbars.

And the helpers following the form:

module.exports = function (handlebars) {
  handlebars.registerHelper('foo', function () {
    return 'bar';
  });
};
@hueniverse
Copy link
Contributor

Is the idea to map the filename (without the js extension) to a single exported function?

So the helpers path will include a 'foo.js' with content:

module.exports = function (context) { return context.something; }

Which hapi will register via:

engine.registerHelper('foo', require('foo'));

@ghost ghost assigned hueniverse May 11, 2013
@Iheartweb
Copy link
Author

Sounds about right. A little more detail though.

Typically, each helper is in a file, rather than all helpers in one file. So, any configuration would have to accept a directory or a file with or without the js. So:

/
--templates
----helpers
------foo.js
------bar.js

helpersPath = __dirname + '/templates/helpers' will load foo.js && bar.js and helpersPath = __dirname + '/templates/helpers/foo' or ...foo.js should just load foo.js.

The 2nd part that makes this tricky is that although nearly all tmpl engines have helpers, they are declared in a slightly different way. And, I'm not sure Consolidate provides a standard interface.

The 3rd part (that makes this a lame request) Using fs to load files is a bit slow. Applications with a glut of helpers will start slowly. Furthermore, Hapi.createServer(...).start() should not be called until all helpers have loaded.

jmonster pushed a commit to jmonster/hapi that referenced this issue Feb 10, 2014
@Marsup Marsup added feature New functionality or improvement and removed request labels Sep 20, 2019
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

3 participants