How do I get Haml to stop from evaluating #{var} values inside <pre> <code> tags?
Asked Answered
G

1

7

I'm using CodeRay and Haml to do syntax highlighting on some pages, and I write a lot of Ruby code. The problem is when I have something like this:

%pre
  %code.language-ruby
    :preserve
      def hello(name)
        puts "Hello #{name}!" 
      end

I keep getting errors because Haml keeps trying to evaluate the name variable inside the string, the #{var} syntax is a pretty common idiom in Ruby code and there aremany places where it is used, but I cannot use syntax highlighting of those codes because Haml wants to evaluate those variables inside the string.

Is there a way to tell Haml to not do that in some places?

Gault answered 26/3, 2012 at 23:0 Comment(1)
Welcome to StackOverflow! Remember to upvote answers that you find useful (including answers to others' questions.) And check (accept) the best answer to your own questions.Linchpin
Z
7

Is there a way to tell Haml to not do that in some places?

By escaping the #:

puts "Hello \#{name}!" 
Zeb answered 26/3, 2012 at 23:5 Comment(4)
Thanx a lot! Stupid me tried escaping everything else apart from the '#'. Now it's all running smoothly and even colored better.Gault
You're welcome. Typically we show our thanks on Stack Overflow by upvoting and/or accepting answers.Zeb
I know that and I tried, but I need 15 reputation to upvote you. If anyone reads this please upvote for me :D (accept answer in 3 mins).Gault
Ty, I just gained enough rep thanx to you to upvote @meagar :DGault

© 2022 - 2024 — McMap. All rights reserved.