superluminary
superluminary

Reputation: 49152

Multiline strings in HAML template helpers

I'd like to be able to pass a multiline string to a haml helper.

I have a function format_code, that accepts a string and a set of line numbers to highlight.

The purpose of this function is to convert raw source into formatted HTML with highlighted lines, line numbers, newlines converted to brs, etc.

If I call it like this:

%p Some HAMl

=format_code("def something
  a = b
  b = c
end", :highlight_line => 2)

%p Some more HAML

The reason for doing this is to embed a code example into a HAML document.

HAML complains about nesting in plain text. The nesting takes place within a string so I had assumed this would be OK. The same call works fine in irb.

I would use a helper, but I want to be able to pass a parameter hash. Any thoughts?

Upvotes: 3

Views: 2606

Answers (2)

Confusion
Confusion

Reputation: 16841

I'm supposing you want to hardcode a code example into a HAML template, so you really want something to which my immediate response is: "you don't want this". In that case, I would probably choose:

:ruby
  code = <<-CODE
    def something
      a = b
      b = c
    end
  CODE

%p Some HAMl

=format_code(code, :highlight_line => 2)

%p Some more HAML

Upvotes: 2

mliebelt
mliebelt

Reputation: 15525

I tried different variants, and I think the easiest one is the following (code taken directly from yours):

%p Some HAMl

=format_code("def something\t  a = b\t  b = c\tend", :highlight_line => 2)

%p Some more HAML

This should at least the problem with multiple lines ...

Upvotes: 0

Related Questions