leshill / handlebars_assets

Use handlebars.js templates with the Rails asset pipeline.
MIT License
648 stars 159 forks source link

Cannot use Handlebars helpers on element tags #59

Closed farias-r7 closed 11 years ago

farias-r7 commented 11 years ago

Not sure if this is a bug, or a limitation of how Hamlbars and Slimbars are compiled

My understanding is that file extensions .hamlbars and .slimbars first go through the HAML and SLIM templating engine, then that generated HTML w/ Handlebars markup is passed to Handlebars for precompilation.

The issue is this:

The Handlebars helpers don't work on a Haml element tag. Probably because the if logic isn't run until the template is compiled by handlebars. Since HAML compilation happens first it breaks.

Not sure on a fix.

uncaught Error: Haml::SyntaxError: Illegal nesting: nesting within plain text is illegal.

Hamlbars template

{{#if true}} 
.notification-message
{{else}}
.notification-message.message-read
{{/if}}  
  .header 
    .title
      {{title}}
    .close
      x
  .content
    {{content}}
    %span 
      2 Days Ago
  .details 
    44 Hosts Found
farias-r7 commented 11 years ago

Looks like an alternative is to use partials. Let me know if this is the suggested approach. I will close the issue if it is.

notification_item_view.hamlbars

{{#if true}}
.notification-message.message-read
  {{> notification_center/item_views/_notification}}
{{else}}
.notification-message
  {{> notification_center/item_views/_notification}}
{{/if}}

_notification.hamlbars

.header 
  .title
    {{title}}
  .close
    x
.content
  {{content}}
  %span 
    2 Days Ago
.details 
  44 Hosts Found
leshill commented 11 years ago

Hi @farias-r7,

Using partials works. Another alternative would be to add a property to your context to set the appropriate classes:

%div(class='{{notificationClass}}')

Set the notificationClass property to either notification-message or notification-message message-read.

farias-r7 commented 11 years ago

Sounds good! Closing issue,