ericf / express-handlebars

A Handlebars view engine for Express which doesn't suck.
BSD 3-Clause "New" or "Revised" License
2.31k stars 383 forks source link

Added option for using the file name for the partial key #100

Closed JasonTowner closed 9 years ago

JasonTowner commented 9 years ago

View README.md I added a configuration option to use the file name of the handlebar templates as the partial name regardless of directory structure. This supports having a sub-components directory structure where each sub-component is in it's own folder containing the handlebars template, scss, js, separate readme, etc. (see the screenshot below). screen shot 2015-01-14 at 11 37 25 am While this is currently supported, the keys for these partials are button/_button and error/_error and this new option would set the partial keys to _button and _error respectively.

trevordmiller commented 9 years ago

@ericf: This would be wonderful! I have struggled with the same problem. It is annoying to use the duplicate keys for Handlebars partials.

ericf commented 9 years ago

This should already be handled by specifying the partialsDir config as an array of string paths to the component dirs: https://github.com/ericf/express-handlebars#partialsdirviewspartials

JasonTowner commented 9 years ago

Thank you for your comment and quick response. My issue with that is that each sub-component would need to be added to the partialsDir array. So a website with 50 front-end components would all have to be added to the array. I think it's great to support that, but I also think we should add an option to do this automatically.

The code currently would have to look like this:

{
    partialsDir: [
        '/components/button',
        '/components/error',
        '/components/footer',
        '/components/header',
        '/components/list-block',
        '/components/video',
        '/components/image',
        '/components/video-gallery',
        '/components/image-gallery',
        '/components/product',
        '/components/product-list',
        ...etc
    ]
}

VS.

Using an option like the one in my pull request

{
partialsDir: '/components',
fileNameAsPartialName: true
}

We can discuss the proper name of the option as well as the proper place to do this in code, but I feel that this is something that should be included. Thoughts?

ericf commented 9 years ago

@JasonTowner you don't have to list these manually, you could use glob:

var glob = require('glob');
// ...
app.engine('.hbs', exphbs({
    extname: '.hbs'
    partialsDir: glob.sync('components/*/')
}));

I think it's much more flexible to leave it open-ended and allow people to compute the array of partials dirs however they need to for their specific app. Since what you want to do is already supported, but it's just getting the array of component sub-dirs, I'd rather not add an option for that use case.

JasonTowner commented 9 years ago

@ericf Good call, I totally spaced that part. I got so caught up on the optional param that I was blinded. Thanks!

ericf commented 9 years ago

:beers: