Creating New Haml Filters

Written by steve on 04-08-2007 at 02:41 PM

Recently (like, yesterday), I decided to create a Haml filter to wrap inline Javascript. If you don’t know about Haml, see here.

What Haml is, essentially, is a drop-in replacement for Rails’ rhtml. What a filter is, essentially, is a chunk of reusable code that filters an arbitrary block of text, performs some transformation and inserts it into the generated (X)HTML stream. An example of a filter is markdown. E.g.:

        
          :markdown
            This is **bold** and *who knows* what this is?
        
      

Ok, with that introduction out of the way, the filter I needed would allow me to insert Javascript inline with a minimum of muss and fuss.

The steps required to create such a filter are (thanks to Hampton, Nathan, and Evgeny):

Create a file that implements the filter (in this case /lib/inline_javascript.rb):

        
        module Haml
          module Filters
            class InlineJavascript
              def initialize(text)
                @text =<<EOS
        <script type="text/javascript">
          //<![CDATA[
            #{text.chomp}
          //]]>
        </script>
        EOS
              end
      
              def render
                Haml::Helpers.find_and_preserve(@text)
              end
            end
          end
        end  
        
      

That all there is to the implementation. The next thing is to inform Haml of the presence of this filter. That is done by setting Haml::Template.options[:filters]. I did this by adding the following lines to the bottom of my /config/environment.rb:

        
        require 'inline_javascript'
      
        # define an additional filter to create inline javascript
        Haml::Template.options = {
          :filters => {
            'inline_javascript' => Haml::Filters::InlineJavascript
          }
        }
        
      

Now, I can do this in my Haml template:

        
      :inline_javascript
        function onLoad()
        {
          // do something
        }
      %p back to regular Haml
        

And the result is:

        
        <script type="text/javascript">
          //<![CDATA[
            window.onload = function () { applesearch.init(); }
          //]]>
        </script>
        <p>back to regular Haml</p>
        
      

So the natural question I asked myself is: Why is this better than a helper? I’m not sure I have a good answer for this, but the occasion for inline javascript across an application can be vast and putting helpers for each specific instance of javascript could be unduly burdensome. On the other hand, if you have the same inline javascript that’s shared all over the place, a shared partial with the inline_javascript filter would be DRY.

Permalink

 

2 comments

On 03-31-2008 at 08:05 PM tommy said:

hey, it should be 'inline_javascript' => Haml::Filters::InlineJavascript the example above gave an error in Rails 2.0

On 03-31-2008 at 11:29 PM steve said:

what you said :) The keys have changed. I've updated the entry. Thx.

name (opt.)
email (req.)
comment
which one does not fit: cat, car, truck, boat

blog home