views:

67

answers:

3

In haml, how do I render the following incredibly basic HTML:

<p>Results found for <strong>search term</strong>

where 'search term' is actually a Ruby variable called @query?

I'm trying the following,

%p results found for <strong>= @query</strong>

But that renders = @query literally. If I try:

%p results found for <strong>
= @query
</strong>

then the query term renders correctly, but is on a new line.

Also, I'm wondering if there's a better way to render <strong> in haml, while keeping everything on the same line.

I'm aware of the haml documentation, but as far as I can see there isn't an example of using a simple inline Ruby variable.

-----UPDATE-------

The following code works, and shows how to use a variable that's not within tags:

   %p   
    = @trials_found_count
    results found for
    %strong= @query

But I find it really unreadable - it's hard to tell that it renders as just one line of HTML without adding a comment above.

Is there a way I can put all this code on a single line? Or is this just how haml works?

+2  A: 

HAML is whitespace delimited. Nested tags go on the line below and one level in from the tag above. Embedded Ruby from which you want to display output is opened with an '='. Embedded Ruby which you don't want to display such as the start of loops uses '-' These are equivalent to <%= %> and <% %> respectively in erb.

What you want would look like this:

%p 
   results found for
   %strong= @query

Which would produce the html:

<p>results found for <strong>@query</strong></p>

It should be noted that the '=' to start Ruby evaluation can only come at the beginning of the line or after a tag declaration and that only one tag declaration can occur per line. The Ruby Evaluation section of the reference you linked covers embedded Ruby in detail and the haml tutorial which covers embedded ruby and many other haml basics is here:
http://haml-lang.com/tutorial.html

nuclearsandwich
I cannot comment on the answer below for some reason. Reputation?Anyway, haml renders the line break between tags as a space in html. At least it has done so in all the haml templates I've ever done ~50 or so.
nuclearsandwich
Since the paragraph tag has more than one line, you cannot have anything else on the same line as it. So "results found for" needs to be on the next line. Also, in older versions, indentation had to be exactly 2 spaces (%strong is at 3). It doesn't appear to be that way any longer, but if everything else is two spaces, it will still be upset about inconsistent indentation. Here is an example that works for me http://gist.github.com/463507
Joshua Cheek
Thank you! What if I want to do %p [NUM_RESULTS] results found for - where NUM_RESULTS is a variable? Do I have to put it on a new line as well? Have read tutorial but really finding haml hard to get my head around.
AP257
Answered my own question... but please see above for questions on layout...
AP257
assuming @results is a collection variable you can simply use @results.count at the beginning of the %p tag.
nuclearsandwich
Joshua, I corrected the syntax in my example. haml does now allow any consistent indentation I chose 3 spaces here to emphasize that indentation was how haml operates. If you think it should be two I'll be happy to change it.
nuclearsandwich
thanks - see edit to original question, about readability - is it really necessary to have all this stuff on different lines? or is that just the way haml works?
AP257
That's the way haml works. It actually becomes very readable after some time with it and it certainly sped up my templating relative to standard html with erb. There are other template engines out there. Markaby was a favorite for one of my friends.
nuclearsandwich
OK. Thank you for all the help :)
AP257
A: 

I'm not sure, but you might need a non-breaking space to preserve the space between the for and the <strong>

%p results found for&nbsp;
   %strong= @query
Patrick Klingemann
A: 

Here's how I'd do it:

%p No results found for <strong>#{h @query}</strong>
nex3