I’m looking for template engine which is best suited for generating javascript code. It means that engine must be specialized on js code (and maybe other langs, but now I focus on js) and must help you to write template that generates knowingly correct js code.

Here is one of my use-cases: generation of Angular.js services for calling backend API. I generate code like this:

service.createSomeEntity = function(
   name, httpConfig) {
   var params = {
      name: name
   var body = {
      method: 'POST',
      url: '/api/someEntity',
      params: params   };

   return $http(angular.extend({}, body, httpConfig));

Currently I use Freemarker template which looks like this http://pastebin.com/Rmki3JRg which is ugly and unpleasant to write. For example, I have to generate function arguments with #foreach like usual text. But specialized engine would know js syntax and could offer tag for arguments which will let me generate them properly formatted and only in function's definition/call. It could even provide special tags for arguments valid inside function's body template.

  • 1
    I don't think you have stated your requirements clearly other than you don't like Freemarker; you haven't even said how Freemarker is ugly or unpleasant. So how does this SR answer fail to meet your need, especially since it addresses the question of generating "correct" (target language) code? softwarerecs.stackexchange.com/questions/37772/… – Ira Baxter Dec 1 '16 at 9:06
  • Freemarker template consists of text generation logic. Like #foreach in function arguments: I don't want to describe how to generate set of function arguments, I want to give my arguments to the engine somehow (maybe in JSON) and get them generated. Because engine is specialized on js code, it knows the syntax and knows that inside function() you may have arguments. Your answer solves the problem, it's just not suitable for me. I'm not asking for any way to do the job - there are a lot of ways, I'm asking for most convenient way for everyday use (like given use-case). – spontaliku Dec 1 '16 at 9:41
  • 1
    Okay honestly I don't want DMS because it looks like big powerful enterprise machine with contact-us-instead-of-download-button. I don't say it is bad, just unsuitable in particular case. If I had my job reliyng on code generation, I will consider it, but for small automation tasks in web development it looks like overkill – spontaliku Dec 1 '16 at 9:52
  • What's wrong with writing modular, paramaterized, code? – Mawg says reinstate Monica Oct 26 '17 at 13:37
  • @Mawg nothing. But in explained case, and in many others, properly modularized code becomes very hard to work with for a programmer. For example, if you have 30 backend controllers with dozen of methods each, then fully parametrized code for given example would look like this. There is no way for an IDE to help you with param type, returned type, everything. And you would have to look at backend docs each time you call it. Why do it when you have more convenient solution? – spontaliku Oct 27 '17 at 14:24

You could have a look at SimpleGen. It is based on Jinja2 and can be extended with custom functionality quite easily. You can even write your own code-generation functions in JavaScript (which may be more familiar to you than FreeMarker).


We have used this in a wide variety of projects (generating boilerplate code for Java-based backend projects, Website projects and even a C#-based Unity game engine project).

Disclaimer: I'm the author of SimpleGen.

  • As far as I understood, SimpleGen is basically a template processing runner. The problem is it does not understand the language it generates. So it's the same as any other template engine (please correct me if I'm wrong) but with convenient runner. – spontaliku Oct 27 '17 at 14:37
  • 1
    You are correct, SimpleGen does not understand the languages it generates. I'm not sure how practical this would be though. What SimpleGen offers is pluggable functions that can generate any code and that are written in JavaScript, so you could write your generation logic in JavaScript instead of Freemarker. Since JavaScript is the language of your project, I figured you might like this more. If you really want to use something that understands the language it creates, have a look at JetBrains MPS, however it has a steep learning curve and seems like total overkill for your purposes. – Jan Thomä Nov 1 '17 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.