8

I have a rather robust project that has all of its templates implemented in Slim and a little Haml. I'm considering moving the project over to a Node.js based structure (probably built atop Tower.js or something similar; maybe Meteor), however I would like to continue using Slim or Haml for the templates rather than shifting over to another template engine.

Has anyone done this or spent the time to figure out how to accomplish this so far, and if so, what were your findings?

Daniel
  • 2,744
  • 1
  • 31
  • 41
ylluminate
  • 12,102
  • 17
  • 78
  • 152

1 Answers1

0

Tower.js wraps the express.js template engine system, so you should be able to use any express.js template engine by adding the following to the configuration Tower.View.engine = "haml"

Tower.js uses mint, which has HAML built in, so you just need the haml module in your directory.

Slim is problematic though. First its not within mint.js directly, so you'll need to register a new template engine. Todo that you'll need to

  1. create a function customAdapter that mint.js can use, you can look at the mint.js sourcecode. Heres an example of the haml adapter function:

    function(content, options, callback) {
      var result;
      result = require('hamljs').render(content, options || {});
      if (callback) {
        callback.call(this, null, result);
      }
      return result;
    }
    
  2. you'll need to add the adapter to mint.js by doing require("mint").engines.slim = customAdapter

But there's no express.js engine for Slim, and since Slim contains ruby code snippets, it isn't likely there will be.
If you really want to do it so you'll need to create your own javascript parser for the template files (probably using something like http://opalrb.org/ , though with no specific ruby libraries), and then create an express.js engine (which I don't know how to).

I doubt it will be a very fruitful exercise.
I suggest you either convert your Slim files to haml (or jade, or any template engine that can be used by express.js) or don't make the move.
I could also not find any way to automatically convert Slim to haml or any other template engine (probably due to the fact that it contains ruby code), so you will have to do the conversion entirely manually.

Alon Bar David
  • 1,757
  • 14
  • 15
  • From what I have heard from the Slim guys, it should be relatively easy to implement with Slim, yet no one has tackled it via its mechanisms. Additionally, would you have any insights wrt shifting this towards Meteor vs Tower as we've essentially given up somewhat on Tower due to the slower coming updates over recent times. – ylluminate Apr 23 '13 at 17:58
  • Sorry I'm not familiar with Meteor. Tower.js is basically just a glue between very common modules, and it uses express.js internally for rendering view. Meteor seems like a much more involved framework, and mixes server and client side functionality. Considering that, I would imagine it will be harder to port an existing application to Meteor then to Tower.js. Templating language is also not as easy to drop into Meteor, though there are some people that already created packages for it (from my very quick search on the matter) – Alon Bar David Apr 23 '13 at 19:33