3

I know that expressjs supports many view engines, like: ejs, jade, etc. I saw a list here: http://expressjs-book.com/forums/topic/how-to-use-alternative-non-jade-template-engines-with-express/.

My question is how can I create my own view engine. I've looked in ejs and in other engines' source-code but I didn't really find an expressjs documentation on how to create a new engine, what are the requirements, or a tutorial about it.

Is it possible to create a new custom view engine? Thanks.

danchohen
  • 121
  • 9
  • 2
    `Is it possible to create a new custom view engine?` Of course, it was done many times. Basically, view engine is some sort of HTML compiler. You could compare view engines with JavaScript or CSS preprocessors, e.g. CoffeeScript/TypeScript or Less/Sass. You would need some knowledge on how compilers work if you want your view engine to be effective. But I don't know why would you go through the trouble, there are already enough view engines out there so why would you reinvent the wheel? – Marko Gresak Jul 29 '14 at 09:05

1 Answers1

2

Yes, of course. Take a look at this list for templating engines.

Regarding express itself, what you need to do is create a plugin, or even a middleware function - that will attach your render, renderFile and similar methods to the response object.

If you don't use an express engine and try to, say, response.render('index.ejs');, you'll get an error. But if that response object has a render method, you're fine. So it boils down to extending expresses' response object with what you need.

Zlatko
  • 18,936
  • 14
  • 70
  • 123