Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HAML nesting tags

Tags:

haml

I'm trying to nest this

  %h1 Admin Menu
    %small logged in as: #{session[:username]}

To get something like this

<h1>Admin Menu <small>logged in as: something</small></h1>

But the only way I can get it to display without firing an error is putting them at the same level, say

  %h1 Admin Menu
  %small logged in as: #{session[:username]}

Which outputs:

<h1>Admin Menu</h1>
<small>logged in as: something</small>

It's probaby something silly but I have no idea this why this wouldn't work?

like image 624
leopic Avatar asked Nov 17 '11 06:11

leopic


3 Answers

Haml only allows inline nesting if everything that will be nested is inline. So you can do this:

%h1 Admin Menu

or this:

%h1
  Admin Menu
  %small logged in as: #{session[:username]}

but not this:

 %h1 Admin Menu
   %small logged in as: #{session[:username]}

The first form should really be thought of as a convenient abbreviation. It exists so you can take something like this:

%li
  One
%li
  Two
%li
  Three

and just say:

%li One
%li Two
%li Three
like image 152
Matt Sanders Avatar answered Oct 01 '22 18:10

Matt Sanders


I haven't tried, but won't the following work for you?

%h1 
  Admin Menu
  %small logged in as: #{session[:username]}
like image 34
Swanand Avatar answered Oct 01 '22 18:10

Swanand


The other trick is this.

%h1 
  Admin Menu
  %small
    logged in as: 
    =session[:username]

because last 2 lines will be rendered inside "small"-tag

like image 44
vekozlov Avatar answered Oct 01 '22 17:10

vekozlov