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

Can I add options for handlebars compiler? #73

Closed
harttle opened this issue Mar 11, 2015 · 8 comments
Closed

Can I add options for handlebars compiler? #73

harttle opened this issue Mar 11, 2015 · 8 comments

Comments

@harttle
Copy link

harttle commented Mar 11, 2015

handlebars will auto-indent partials. It becomes undesirable in <pre> and markdown. See this issue:

handlebars-lang/handlebars.js#858

handlebars compiler added preventIndent option, see:

handlebars-lang/handlebars.js@632fadc

I'll appreciate it if you can provide someway to add this compiler option, thanks.

@mgutz
Copy link

mgutz commented Mar 11, 2015

please test branch issue-73, see test/issues.js

@harttle
Copy link
Author

harttle commented Mar 12, 2015

great work! it works fine.

@mgutz mgutz closed this as completed in 147aac1 Mar 12, 2015
@ErisDS
Copy link
Member

ErisDS commented Mar 15, 2015

Hey, I tried to implement this new compiler option in Ghost, by upgrading express-hbs, and adding the onCompile function here: https://github.com/TryGhost/Ghost/blob/master/core/server/middleware/index.js#L55

However, the option is going missing somewhere. I'm trying to trace it through and I can't figure out what's going wrong. In compiler.js, the option is present in compileInput but not in PartialStatement - so I'm thinking this is a handlebars issue rather than a problem here, but thought I'd start here in case someone knows different.

@mgutz
Copy link

mgutz commented Mar 15, 2015

I'm not seeing onCompile overridden there. Is it on a different branch?

@ErisDS
Copy link
Member

ErisDS commented Mar 15, 2015

@mgutz Sorry, it's here: ErisDS/Ghost@cf9eb5b

It's identical to the example code and the express-hbs test for the functionality works for me just fine. Very odd.

@ErisDS
Copy link
Member

ErisDS commented Mar 20, 2015

I got this figured out :)

The example of onCompile wraps the passing of preventIndent in an if statement which checks whether the thing about to be compiled is a partial:

  onCompile: function(exhbs, source, filename) {
    var options;
    if (filename && filename.indexOf('partials') > -1) {
      options = {preventIndent: true};
    }
    return exhbs.handlebars.compile(source, options);
  }

However, the preventIndent option needs to be applied to the template which calls the partial, rather than the partial itself, as far as I can tell.

Therefore the following works as expected:

  onCompile: function(exhbs, source) {
     return exhbs.handlebars.compile(source, {preventIndent: true});
  }

@mgutz
Copy link

mgutz commented Mar 20, 2015

That makes sense. I was showing how to filter using the filename. I apologize for the side effects of passing that filename though. Caused at least two issues for you.

@ErisDS
Copy link
Member

ErisDS commented Mar 21, 2015

No problem, I'm enjoying getting more familiar with the express-hbs codebase :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants