Partials in Coffee HAML (.hamlc)

放肆的年华 提交于 2019-12-20 18:34:36

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/9201593/partials-in-coffee-haml-hamlc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!