jsf
jsf

Reputation: 2971

Is it a good idea to mix Moustache/Handlebars Templating with HAML?

I would like to know if it's a good idea to mix Moustache or Handlebars templating with HAML. If yes, what would be the benefits and disadvantages. Why I am asking this? I have seen brogrammers mixing templating. Thanks.

Upvotes: 4

Views: 803

Answers (1)

Jan
Jan

Reputation: 1000

I am generating mustache templates with HAML without any problems.

My HAML looks kinda like this:

.template.shop
  .foreground
    .header
      .header-logo
    .shop-description
      .table-frame
        %a.close-button{:href=>"#"} Close
        %table
          %tr
            %td.shop-images
            %td.shop-details
              %p.popup-headline.fix-width
                {{addr_name}}
              %p
                {{addr_street}} {{addr_number}}
                %br/
                {{addr_zip}} {{addr_city}}
              / {{#hasSchedule}}
              %p.popup-headline
                \Öffnungszeiten
              %table.shop-schedule
                / {{#sched_mo}}
                %tr
                  %td Mo:
                  %td {{sched_mo}}
                / {{/sched_mo}}

                ... Rest of Table omitted

This works like a charm. It gets rendered as HTML on the server which I extract from the page using jQuery on the client. I then take the HTML of the template and render it using mustache.

Because of this, you sometimes need to be careful where the mustache tags occur. For the browser they're just text so they're invalid between table rows for example. In this case put them in HTML-comments like in my example.

You could also escape the generated HTML/Mustache on the server. That wasn't feasible in my case because I wanted to have the generated template part of my HTML for easier styling.

Upvotes: 3

Related Questions