咖啡 HAML 中的部分 (.hamlc) [英] Partials in Coffee HAML (.hamlc)

查看:13
本文介绍了咖啡 HAML 中的部分 (.hamlc)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 HAML Coffee 的 rails 后端使用主干.strong>,由 haml_coffee_assets 编译.我的模板中有一些重复.

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.

有没有办法创建类似rails 的部分来干我的模板?

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

补充:我可以在 Coffee HAML 中做 content_for(:something) 吗?

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

推荐答案

Haml Coffee 中没有 content_for 助手,但您可以简单地在模板中渲染另一个模板.

There is no content_for helper in Haml Coffee, but you simply can render another template within 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

诀窍是使用 != 对呈现的 HTML 进行转义.

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

要传递局部变量,只需将它们发送到 JST 函数.如果这是你的部分(articles/_comments.jst.hamlc):

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

%h2=@title
%p=@content

那么这可能是你的模板:

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

这篇关于咖啡 HAML 中的部分 (.hamlc)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆