using the :escaped filter in #HAML

by Mike Zazaian at 2009-08-10 19:14:53 UTC in templating

a useful tip for printing unrendered code to a web page

no comments no links ["

I was writing a little markdown key the other day for the comments section that you'll see at the bottom of this page (assuming that you're a do{block} user and that you're logged in to the site, and hit a bit of a snag when I was trying to demonstrate that you can place \"code\" tags around code to make it appear as such.

\n\n

Problem is, #HAML seems to interpret code tags automatically for you, regardless of whether they're included in a string or just typed directly into a template. As you might imagine, I spent a bit of time fussing with various escape characters and conventions, to no avail, until it occurred to me that I might find an answer in #HAML's reference guide (which, by the way, is great and wonderful and a read as riveting as any three Mike Nichols films).

\n\n

Anyway, it's simple.

\n", nil, "
\n
#!/app/views/articles/show.haml\n\n:escaped\n  <codetag> @this = is :ruby_code </codetag>
\n
\n", nil, "

Please note that if you're actually demonstrating code in a comment your tags should read \"code\" rather than \"codetag\". I did this only because, I ironically, I haven't developed a mechanism to escape the plain \"code\" tags from tm_syntax_highlighting.

\n\n

Anyway, there you go. Anything that you put under that escape filter will be entirely escaped and won't be rendered in any capacity. I hope it'll save you the hour that it cost me.

\n"]

no comments

login to post comments, or register to post a comment

latest links

Help.GitHub - Multiple SSH keys The article from github help mirroring this process
ones zeros majors and minors ones zeros majors and minors: esoteric adventures in solipsism, by chris wanstrath
ActiveScaffold A Ruby on Rails plugin for dynamic, AJAX CRUD interfaces

login

register activate reset

feeds

articles/rss

topics

staff

editor

about

doblock focuses on ruby, rails, and all things that can help ruby and/or rails programmers hone their skills.

Techniques, tutorials, news, and even free open-source applications, doblock seeks to fill in the cracks of the ruby/rails blogosphere.

doblock v. 0.10.1 powered by Rails