Convert String to HAML

415 views
Skip to first unread message

bilson

unread,
Jan 12, 2008, 9:22:52 PM1/12/08
to Haml
Is there a simple way to use HAML markup for a content management
system? Basically I just want to...
- Enter haml markup into a textarea and save to a database
- Retrieve the text string from the database and display as HTML

I don't see a helper that takes a string of HAML markup and returns
HTML, but I'm probably overlooking it. I guess the HAML lab is
essentially doing this, so there must be a way. I would also like to
suppress evaluation of Ruby code for this facility. Is there anyway
to suppress_eval only for specific controllers or views or does it
have to be application-wide?

BTW - HAML is the greatest plugin ever.

Best Regards,
Bilson

bilson

unread,
Jan 13, 2008, 3:45:05 AM1/13/08
to Haml
I have this sort of working, though I doubt this is a very good way.
I added a parse_haml method that I found posted for a different use.
I threw the method into application.rb and made it a helper...

application.rb
-----------------

def parse_haml(string)
engine = Haml::Engine.new(string)
engine.render
end
helper_method :parse_haml

Now I can pull a string formatted as Haml out of the database and
display it appropriately. Even cooler is that I added a form observer
and have a live preview of the textarea going as I type. I also made
a background image for the textarea that shows 2 space columns to make
the textarea into a usable text editor.

The one thing that doesn't work is that none of the ActionView helper
methods are available. Is it possible to reinsert ActionView into the
chain?

Best Regards,
Bilson

bilson

unread,
Jan 13, 2008, 4:00:33 AM1/13/08
to Haml
This live preview is awesome. Adding the :ruby filter is better than
using script/console. Live testing. Haml rocks!

Best Regards,
Bilson

Nathan Weizenbaum

unread,
Jan 13, 2008, 6:28:44 PM1/13/08
to ha...@googlegroups.com
Check out the docs for Haml::Engine: http://haml.hamptoncatlin.com/docs/rdoc/classes/Haml/Engine.html. It should answer all your questions, but if it doesn't, feel free to ask again.

bilson

unread,
Jan 18, 2008, 7:03:11 PM1/18/08
to Haml
I read the Engine docs, but I'm not picking up on how I get the
helpers like "link_to" and "image_tag" to work. It is recognizing
Ruby code and Haml tags and converting to HTML, but not Rails helpers.

Best Regards,
Bilson

On Jan 13, 6:28 pm, "Nathan Weizenbaum" <nex...@gmail.com> wrote:
> Check out the docs for Haml::Engine:http://haml.hamptoncatlin.com/docs/rdoc/classes/Haml/Engine.html. It should
> answer all your questions, but if it doesn't, feel free to ask again.
>

Nathan Weizenbaum

unread,
Jan 18, 2008, 7:12:40 PM1/18/08
to ha...@googlegroups.com
The way Rails helpers work is that they're defined for the ActionView::Base class. This class is then instantiated, and it's used as the context in which the template is evaluated. To specify such a context for Haml, you need to pass the ActionView::Base object to Haml::Engine#render. Note that if you're doing this from within a helper, "self" is the current ActionView::Base instance.

On Jan 18, 2008 4:03 PM, bilson <baldwi...@gmail.com> wrote:

I read the Engine docs, but I'm not picking up on how I get the
helpers like "link_to" and "image_tag" to work.  It is recognizing
Ruby code and Haml tags and converting to HTML, but not Rails helpers.

Best Regards,
Bilson

bilson

unread,
Jan 18, 2008, 10:29:25 PM1/18/08
to Haml
Sorry, I'm not as smart as the other kids. ha.
Thanks. It's working perfectly now with...
= Haml::Engine.new(@stuff).render(self)

Best Regards,
Bilson

On Jan 18, 7:12 pm, "Nathan Weizenbaum" <nex...@gmail.com> wrote:
> The way Rails helpers work is that they're defined for the ActionView::Base
> class. This class is then instantiated, and it's used as the context in
> which the template is evaluated. To specify such a context for Haml, you
> need to pass the ActionView::Base object to Haml::Engine#render. Note that
> if you're doing this from within a helper, "self" is the current
> ActionView::Base instance.
>
Reply all
Reply to author
Forward
0 new messages