Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a strong element inside a p

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?

like image 480
AP257 Avatar asked Jul 04 '10 14:07

AP257


3 Answers

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
like image 45
Patrick Klingemann Avatar answered Oct 03 '22 14:10

Patrick Klingemann


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

like image 177
nuclearsandwich Avatar answered Oct 03 '22 12:10

nuclearsandwich


Here's how I'd do it:

%p No results found for <strong>#{h @query}</strong>
like image 20
Natalie Weizenbaum Avatar answered Oct 03 '22 14:10

Natalie Weizenbaum