ruby-on-railsbackbone.jscoffeescripthamljst

Partials in Coffee HAML (.hamlc)


I am using backbone.js on a rails backend with HAML Coffee, which is compiled by haml_coffee_assets. There is some duplication in my templates.

Is there a way to create rails-like partials to dry up my templates?

Addition: Can I do content_for(:something) in Coffee HAML?


Solution

  • There is no content_for helper in Haml Coffee, but you simply can render another template within a template.

    Without Local Variables

    For example, you've a template test:

    %p My Partial
    %ul
      %li Is included
    

    You can include it within another template like this:

    %p Another template
    != JST['test']()
    %p That includes a partial
    

    The trick is to unescape the rendered HTML with !=.

    With Local Variables

    To pass local variables, just send them to the JST function. If this is your partial (articles/_comments.jst.hamlc):

    %h2=@title
    %p=@content
    

    Then this may be your template:

    %h1 Comments for this article
    - for comment in @article.comments 
      != JST['articles/_comment'](comment)