ericf / express-handlebars

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

Indent option. Mmmmh, indent option! #48

Closed MarcDiethelm closed 10 years ago

MarcDiethelm commented 10 years ago

Since Handlebars doesn't offer this functionality it would be truly awesome if you could add the functionality to exp3hbs. A function implementing this could be as simple as:

    /**
     * @param {string} sInput
     * @param {number} levels – How many levels of indentation to apply
     * @param {string} [char] – Character to use for indentation, defaults to tab char.
     * @returns {string}
     */
    function indent(sInput, levels, char) {
        var indentedLineBreak;

        char = char || '\t';
        indentedLineBreak = '\n'+ ( new Array( levels + 1 ).join( char ) );

        return indentedLineBreak + sInput.split('\n').join(indentedLineBreak);
    }

res.render('foo', {indent: 2}) for example would render the foo view with two levels of indentation. Indentation would be completely optional of course, as a performance hit must be expected obviously.

Let me know if you're interested in a pull request. :)

There was a pull requet at Handlebars for this: https://github.com/wycats/handlebars.js/pull/237

ericf commented 10 years ago

@MarcDiethelm This isn't something that I am interested in adding. In one of the recent versions of Handlebars, whitespace control was added, so my suggestion would be to use that feature to handle things like this; and it will work transparently with this package.

MarcDiethelm commented 10 years ago

I was afraid you were going to say this. Unfortunately that's not what Handlebars' whitespace control does. It can only eliminate whitespace (introduced around the control structures). As it stands we have no way of indenting views. To me it's as frustrating as Handlebars synchronous nature.