Skip to content

[REQ] Template engines mixing: Mustache + Handlebars + etc #6567

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

Open
OnkelTem opened this issue Jun 5, 2020 · 1 comment
Open

[REQ] Template engines mixing: Mustache + Handlebars + etc #6567

OnkelTem opened this issue Jun 5, 2020 · 1 comment

Comments

@OnkelTem
Copy link

OnkelTem commented Jun 5, 2020

Consider this regular scenario.

I generate API front-end code using typescript-fetch generator. Its templates are written in Mustache engine. I do my best editing templates to get the appropriate code, but since no logic is supported by Mustache, and I'm extremely limited. I cannot even write a simple if!

For instance, I cannot do:

{{#if model.name = "MyModel"}}
  // Generate something
{{/if}}

to build some custom type, array or enum. IIUIC, the only way to achieve this, is to edit the generator's java code to add an auxiliary boolean variable ­— e.g. isModelMyModel — to then use it as {{#isModelMyModel}}...{{/isModelMyModel}}.

Sure I can do:

{{#model}}
  if ('{{name}}' === "MyModel") {
    // Generate something
  }
{{/model}}

which would turn into a bunch of:

  if ('NotMyModel' === "MyModel") { // Always false
    // Generate something
  }
  if ('MyModel' === "MyModel") { // Always true
    // Generate something
  }
  if ('AnotherModel' === "MyModel") { // Always false
    // Generate something
  }
  // etc...

and it would be terrible.

This task could have been easily solved by using a templating engine which has a little more sense than Mustache in this particular case. And here comes Handlebars, hurray!

But as far as I see from googling the only way to use it is to completely switch the engine using -e generator option. So there is no such an option as combining Mustache and Handlebars. And since all the openapi generators are written in mustache we're still deprived of HBS, no matter how good it is. From the other side, rewriting all the templates from scratch just doesn't have any sense because Mustache already makes it job well... except where it doesn't.

Which leads to one obvious and most reasonable solution of this problem: all the template engines must be supported at once. User just provides the order of preference.

@jimschubert
Copy link
Member

@OnkelTem we won't be mixing Mustache and Handlebars, as this will add confusion (especially for template inclusions). We've actively worked to avoid allowing Mustache and Handlebars from being colocated and compiled through the same engine.

Luckily, Handlebars can process pretty much all of Mustache syntax, excluding custom mustache lambdas. This means converting to handlebars in some cases is as easy as changing the file extension.

For this scenario, if you do want Handlebars and Mustache compilation, I'd recommend creating a custom engine and applying your preferred logic within the adapter. You can see an example I created for the Pebble templating system: https://github.com/jimschubert/pebble-template-engine-adapter You could use our built-in handlebars and mustache adapters, and this gives you the added flexibility of adding customizations to each engine.

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

No branches or pull requests

2 participants