dantebronto / picard

A Micro-framework for node.js
216 stars 12 forks source link

helper functions in haml #10

Closed danieldkim closed 14 years ago

danieldkim commented 14 years ago

need a mechanism to make helper functions accessible from haml code.

i've committed a simple solution in my fork:

http://github.com/danieldkim/picard/commit/95ebf42150a7fc78de2e73157f47bcf53821af6a

please merge if it looks reasonable.

danieldkim commented 14 years ago

ok, the solution above wasn't quite enough for my needs. i've implemented something a bit more interesting in:

http://github.com/danieldkim/picard/commit/5cba5a48c308b07e786093c09d6fe25df7812034

this makes the request scope available to the mixin functions through this.

danieldkim commented 14 years ago

you can ignore the previous comment as that commit was just a more convoluted way of providing what the previous commit already did. argh, it's way past my bedtime ...

dantebronto commented 14 years ago

Thanks for your contributions Daniel. I still haven't had a chance to look into this yet, I'll try and take a gander this weekend.

danieldkim commented 14 years ago

np, dante. happy to contribute. this little change allows me to do something like this in my configuration:

picard.env = {
  root: __filename.replace(/\/config\/environments\/\w+.js$/, ''),
  mode: 'development',
  port: 9900,
  public_dir: '/public',
  views: '/views',
  scope_mixin: {    
    is_blank: function is_blank(var_name) {
      with(this) {
        try {
          var the_var = eval('(' + var_name + ')');
          return the_var == undefined || the_var == null || the_var == '';
        } catch (e) {
          if (e.constructor.name == 'ReferenceError') return true;
          else throw Error("Caught exception when eval'ing " + var_name + ":" + e);
        }
      }
    }
  }  
}

and then use that is_blank() function in my haml code.

btw, there's a few other things that i've built on top of picard in my own project that may or may not make sense to pull into picard in some form (although the more of this stuff that picard takes on the less "micro" it becomes):

dantebronto commented 14 years ago

Today's release of several commits (soon to be 0.2 release) Resolves this issue. Merging custom objects with the view scope is now supported by calling helpers().

danieldkim commented 14 years ago

thanks, the helpers functionality works nicely. i also like the improved exception handling, toned-down logging, template caching (though there is an issue with that that i'll log shortly), and less-verbose haml syntax -- overall, a great release!