Express 3.x best layout implementation (template engines) - node.js

From what I've read, ExpressJS 3, dropped support of layouts, leaving it to the template engines. So if an engine, doesn't have a support for layouts, what's the best Node.js module that will have it? Or if best sounds subjective, not best but at least a working solution?
I'm going to use Hogan.js as a template engine.
Or, maybe there's a better alternative to layouts as a concept? I find it rather helpful but will like to read about other approaches.

There are plenty of template engines.
Here are some good ones I know:
Handlebars - very good extention of {{mustache}}.
Jade - very powerfull template engine from visionmedia (the author of express.js) and my favorite one.
ejs - another template engine from visionmedia.
You'll find more engines here - in this question.

You can take a look of the following templates that is supported in node.js:
https://github.com/joyent/node/wiki/modules#wiki-templating
Also this is benchmark that compares different types of templates according to thier rendering time
https://github.com/Deathspike/template-benchmark
My suggestion:
ejs and jade is very powerful and have a lot of features and both are supported in express

I'm using express-ejs-layouts and works perfectly with underscore/lodash and ejs templates.

Related

Which express templating engine should I use for simple code examples?

I'm writing some example code using node and express, that is targeted at beginner to intermediate front end developers (not node.js or express developers). Basically some plug and play code that allows a beginner dev to get right to the HTML, CSS and behavioral JavaScript, and host it on a simple hosting solution like Heroku.
I need a recommendation for a an express compatible templating language that is very light and simple to understand.
I need:
Variables embedded in the template.
Loops and if statements embedded in the template.
I want to avoid:
Unusual syntax (Jade).
Files that are not named .html (like
mustache).
Unusual looking configuration (for example to force mustache to use .html as an extension).
I need something really basic. Should I role my own, or is there a lightweight, well support third party solution I should be aware of?
I would check out EJS. It is pretty decent and fairly easy to use. It is also compatible with Angular. I go back and forth using EJS and Dust.
EJS is very forgiving and easy to get started.

Use Mustache instead of Razor in OrchardCMS

Is it feasible to use Mustache templates (http://mustache.github.com) in a theme instead of the default Razor syntax?
As most frontend dev's we work with are on a Mac having to use Razor is creating a bottleneck in development, and I would prefer use a templating engine that can be rendered cross platform.
You can in principle use any view engine that is compatible with ASP.NET MVC, but you really shouldn't. You are going to fight with the system every step of the way. All of the themes and modules that you'll find (and you'll need them), as well as all the views in the core, are going to be using Razor. It's just a very, very bad idea.
You should try to find a CMS that uses Mustache natively as its default templating language, or learn Razor.
You can use it if you're willing to write some view engine services (what has some gotchas) and you can use multiple view engines side by side (i.e. you can keep the existing Razor templates and write your own templates in whatever you want).
The result won't necessarily be appealing but you can do it just as it was done for PHP.

Template Engine in NodeJS & BackboneJS

currently i am playing around with NodeJS (+ ExpressJS) and BackboneJS.
The Backbone pages told me (including the Todo Example) that it's better to use a template engine such as EJS or MustacheJS. But i am already using Jade which comes with ExpressJS.
Are Front-End (e.g. Mustache) and Back-End (e.g. Jade) template engines completely different or is it possible to use one for both ?
Or did I not understand something right?
You can use the same template engine for both frontend and backend (we are using it in a project for both sides). Also with care, its easy to switch some forms across as well. To compile jade to client, see
https://github.com/visionmedia/jade#browser-support
very easily this could be incorporated as grunt task, i have put the code in
https://gist.github.com/2877717
Jade works on the front and back-end. You still need to create client-side and server-side templates though.
Generally, if you are already are already using Node.js and Express.js, there is little need to do front-end templating, since you can already control everything server-side. Mixing both is usually unnecessary and very tricky.
As an aside, I would recommend using EJS for back-end templating.
EDIT: Apologies, I think I skirted the complexity of the question a bit. Yes, you can definitely use both, and in the cases of large complex data sets you definitely should use at least a front-end templating engine like Mustache. If you are just getting started with ExpressJS, it's best to stick with just a back-end templating engine like Jade or EJS until you are more comfortable with how back-end templating works—then move to using both if you see it fit.
Thanks to Pickels for calling me out on that!

Node.js without a template engine

I'm new into Node.js and trying to learn. From what I have understood it's common to use a template engine (eg. Jade), even for CSS (eg. Stylus). To be honest, all the tutorials I have seen out there involve a template engine when it comes to the layout.
The thing is that I don't want to use a template engine because I think it's unnecessarily complex.
Here is a link to a boilerplate for Node (unfortunately it doesn't work by some reason), but Option 1 shouldn't be using any template engine for the layout.
So, what is the easiest way to combine Node.js and Mongodb with "normal" HTML(5)? Is it possible for example to use HMTL5 Boilerplate with Node?
If you are using static html, so you wont need templating on the server side.
You can easily serve your html files with Express/Connect static middleware, for example:
app.use(express.static(__dirname + '/public'));
then put an index.html to your public folder.
Also I think you can copy and paste the whole h5bp to your public folder and it should work.
Here are my thoughts on this.
If you are serving only static html, it's obvious that you don't need any template engine, since you can just buffer the html in the response, or use the Connect static middleware.
However things get interesting when you have to deal with dynamic content.
This is where template engines are good at, since they provide ways to integrate your data with the html. If you are going to replace the template engine, you need a library that can do HTML and DOM manipulation. I can think of two alternatives:
jsdom , and libraries that are build on it (such as fill.js).
With jsdom you can use server-side jQuery to build your views, or even YUI.
However it has some drawbacks:
it's slow and cumbersome
it's a pain to install on Windows since it depends on native modules
i couldn't get it to parse html fragments or incomplete html (maybe someone knows a way around this)
The second alternative would be to use some lightweight libraries that handle html, without the full DOM. So far I've found two libs that are good at this:
cheerio - a small library that relies on jQuery -like selectors
plates - a library that binds data to markup
Both are very neat in my opinion, and a good starting point in getting rid of templates :)
There may be others that I'm not aware of, but you get the idea.
Using express, you would just send the html5 in the response:
app.get('/', function(req, res){
res.send('<header>Hello World</header>');
});
However, I would say that in most cases a templating engine doesn't add complexity. The separation of concerns makes things simpler if you are rendering dynamic content.
First time answering my own question. I just want to share that I found a converter from html to jade (template engine). This is definitely a good thing that's removing a lot of complexity at least for me, even if it still involves a template engine.
http://html2jade.aaron-powell.com/

Browser compatible NodeJS templating

I am looking to reuse templates I write for NodeJS in the browser, by this I hope to gain fast page loading time (initial rendering), and dynamic content switching ability, etc...
This would be most natural on Node, any ideas of compatible templating engines?
By the way, I like the HAML style syntax, so anything HAML based is a huge plus.
Thanks!
Roman
Edit:
I think that I like the approach of Mustache the most, it works with Express and also in client side.. will update here on how the implementation went..
Jade is excellent, very similar to HAML and compiles down to JavaScript for browser usage :)
See the README on the Github repo for details.
Jade is also the semi-officially recommended template language for the currently most popular web framework for Node, Express (in fact, they share authors), so it looks like a solid choice to me.
I recently evaluated a lot of NodeJS template libraries and ended up picking Jade.
I've made a simple example of using Jade in the browser. The fun stuff happens in demo.js, and you can also view index.jade which contains the basic page markup.
EJS works in the browser and with express out of the box. Admittedly the syntax is not HANL style nor is particularly feature rich but you can easily get it to work.
I've also used a simple technique to re-use templates and views on both client and server and it works reasonably well
JAVASCRIPT TEMPLATE SHOWDOWN!~
Which is pretty much all the best templates lined up to test in the browser.
(also #Raynos it includes an example of jade in the browser)
Personally I use Jade, combined with stylus and jquery I only use css selectors.
But. . . as you can see from the chart Jade doesn't get along with firefox 3 or opera.
You could try weld https://github.com/hij1nx/weld that has implementation in server side and client side.
node-jqtpl is your best bet. It's a port of jQuery Templates, so 100% reusable in the browser.
There is a command-like utility TplCpl written in Node.js that allows to compile Jade templates for browser use.
https://github.com/jsmarkus/tplcpl

Resources