Nathan Weizenbaum is the man.

Posted by jeff Fri, 18 Apr 2008 13:24:00 GMT

Nathan is part of the team that gave us Haml and its obvious that he genuinely cares about it and the people using it. He made a post on his blog called "Haml Errors Suck" helping to explain the sometimes cryptic error messages that Haml throws and also asking for complaints, advice, etc. about those messages and other problems with Haml.

I was suffering from the no-output error that can occur when your Haml template is malformed in Rails pre 2.0.2. And it looks like Nathan spent the better part of yesterday making Haml work smoothly with versions of Rails all the way back 1.2.X.

Nathan's humility and hard work make him one of my personal Ruby Heroes. Thanks, Nathan.

render_to_string

Posted by jeff Wed, 26 Mar 2008 12:28:00 GMT

Rendering a view in Rails to a String

This is old hat for pros, but this is such a useful method that I wanted to bring it up for anyone googling for this functionality (as I was months back).

Occasionally you'll want to render something in rails and keep the result in a string (to save into a db or file or to display later) rather than displaying it immediately. If you try to make a call to render and then go about your business redirecting or displaying another view you'll get a wonderful ActionController::DoubleRenderError telling you "Can only render or redirect once per action". You want to use render_to_string.

Example: rendering a haml template that has been loaded into template_content

rendered_result = render_to_string( :inline => template_content,
  :type => :haml,
  :locals => { :blah => @blah }
)

Write your Typo posts in Haml - A TextFilter plugin 6

Posted by jeff Fri, 07 Mar 2008 16:20:00 GMT

Update #1: This plugin is now hosted on rubyforge. Please see the official HamlFilter plugin page and the rubyforge page

Update #2: This plugin is mentioned (and slightly mocked) on the Rails Envy Podcast. I've long enjoyed the Rails Envy Podcasts, so I appreciate the nod and joke.

Haml (HTML Abstraction Markup Language) is awesome. If you don't know just how awesome Haml is, you need to check it out asap. It'll speed up your templating dramatically.

Typo is also awesome. As a typo noob I discovered that sadly the two don't play well with each other when it comes to writing posts. I wanted to type my blog posts in Haml. And now I'm in luck and you are too, because this post is written in 100% Haml.

Introducing the Haml textfilter for Typo

I'm still new to Typo, so the packaging and install is ... well, there isn't any. That will hopefully change soon, but until then, you can infuse your typo blog with Haml-y goodness by doing the following.

  1. sudo gem install haml
  2. Download typo_textfilter_hamlfilter-0.1.tar and extract the folder to /your_typo_install/vendor/plugins
  3. Run the following sql against your typo database
    insert into `text_filters` (`name`, `description`, `markup`, `filters`, `params`) values('hamlfilter','HamlFilter','hamlfilter','--- \r\n- :hamlfilter','--- {}\r\n');
  4. Restart your typo server
  5. Via the typo Administration page, select Settings->Write and then select HamlFilter as your Article Filter. You can now do nifty stuff like
  6. ='Hi!' * 10
    and get
    Hi!Hi!Hi!Hi!Hi!Hi!Hi!Hi!Hi!Hi!
    =Time.now
    and get
    Thu March 07 14:25:55 -0500 2008 
    Haml evaluates your ruby code as expected. Because of this, you can do some neat things, but if you're not careful and allow HamlFilter to control your comments, you're in trouble:
  7. Do not, I repeat, do not choose HamlFilter as your comment markup. If you do so, you're subjecting yourself to all kinds of malicious attacks.

After following these steps, everything should work as expected*. If your post is non-Haml-ized, there's probably a flaw in your spacing somewhere -Haml loves proper whitespace. You may want to write posts in your favorite text editor or something more Haml-friendly. A small price to pay for being free to Haml in Typo.

*One more note: Currently, to accomplish typo magic using things like <typo:code>, you'll need to use Haml's :preserve function like so:


  :preserve

      <typo:code lang="ruby">

      class Person

        attr_accessor :name



        #alias for readability joy

        alias knows? respond_to?



        def initialize(name)

          @name = name

        end

      end

      </typo:code>

gives you
class Person
  attr_accessor :name

  #alias for readability joy
  alias knows? respond_to?

  def initialize(name)
    @name = name
  end
end
This should be done automatically in future versions.

Enjoy and check back for updates as I formalize this thing. I'm hanging out in #typo on freenode trying to figure out how to better implement this, so hopefully I'll have a better solution soon.