Question

I'm using middleman to do some rapid prototyping and can't for the life of me figure out how to include one HAML file into another HAML file.

I can include stuff in a layout file, but can't get one non-layout file to include another non-layout file. There are blocks of HTML that I want to reuse on some pages and I think I could do this. I've tried:

- render: partial=>"shared/nav.haml"
=shared/nav.html
="shared/nav.html

and none of these work.

Am I missing a config option or plugin? This is a fresh middleman install.


ANSWER

Partials may need file names that start with an underscore. My partial is placed in a folder called shared. The full name of the file is _nav.html.haml

This worked for me.

!= haml :"shared/_nav"

Example in context:

#email.main.subscriber.resize
  #bg-wrap
    %div
      %img{:src=>"images/backgrounds/image.png",:alt=>""}
  %section#zone10
    != haml :"shared/_nav"

You may also use the format specified in the approved answer below.

Was it helpful?

Solution

I've been using HAML with MiddleMan and couldn't be happier. Here is what is working for me:

I have a file: source/_donate_buttons.h

 #DonationButtons
   %p= t('searching.donate_cover_costs')
   %br
   = partial(:paypal_donate_button, :locals => {:amount => 1, 
     :amount_text => t('searching.donate_1')})

This uses the partial statement shown to include a file called source/_paypal_donate_button.html.haml.

And I include the _donate_buttons.html.haml file itself in a couple of places with:

= partial "donate_buttons"

though I think this could also be:

= partial :donate_buttons

I.e. I think partial is the magic you're looking for.

And, just for completeness, here is a slightly stripped down _paypal_donate_button.haml which shows how the paramaterization works there:

-btnclass = (locals.key?(:highlight) && locals[:highlight] ? "HighlightedDonationButton" : "DonationButton")
-btnstyle = locals.key?(:button_style) && locals[:button_style]
.DonationButtonContainer
  %form{:action => "https://www.paypal.com/cgi-bin/webscr", :method => "post"}
    %input{:name => "business", :type => "hidden", :value => "payments@example.com"}
    %input{:name => "cmd", :type => "hidden", :value => "_donations"}
    %input{:name => "amount", :type => "hidden", :value => "#{amount}.00"}
    %input{:name => "currency_code", :type => "hidden", :value => "USD"}
    %input{:class => btnclass, :alt => t('paypal.alt_text'),
      :style => "cursor: pointer; font-size: 18px; #{btnstyle}", :type => "submit", :value => amount_text}

Fwiw, I don't think the file needs to be _filename.html.haml and can instead be _filename.haml. Also, I'm localizing these, so ignore the t('tagname') and just put strings there. (I didn't want to introduce an error copy-pasting the examples so I left them in there.)

Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top