Use Mustache instead of Razor in OrchardCMS - 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.

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.

Express 3.x best layout implementation (template engines)

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.

Client-Side Rendering: How to leverage server/client side templates?

I'm currently implementing a ruby/sinatra application using erb as the rendering engine using client-side rendering using handlebars/mustache templates as well. I'm implementing a certain list view in which the list elements will be rendered using the client side solution when javascript is present and fallback to server side when not (targeting crawlers this way). But I'm left with the folliowing issue: I basically have two templates written in two different markups which produce the same html. I'd love to "write once, run everywhere". How are other projects leveraging this issue?
I've used sinatra-mustache with Sinatra, and it works great. I have not used it for both client and server side templating from the same template files, but you should be able to do that as well.
I've found the answer to be using your favorite templating flavor on the server-side but using some other templating engine for the cases where the template is going to be shared on the client side. So, I'm using erb most of the time, the rest of the time using poirot (for Rails). This integrates Mustache templates on action pack, which handles multiple templating engines anyway. Poirot comes ready with helpers to write the templates on the document and also client side rendering libraries. It also integrates handlebars and hogan templates, which are mustache extensions.
As for Sinatra, I try using the same approach using erb and https://github.com/defunkt/mustache , but the nice integration from poirot on rails has to be done by hand.

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/

Resources