【问题标题】:Nested render in haml嵌套渲染在haml
【发布时间】:2015-03-09 10:06:03
【问题描述】:

在“show.html.haml”中,我有两个这样的渲染:

= render 'project_header', :locals => {@set_tab => "current"}
= render 'project_toolbar'

我的“_project_header.html.haml”看起来像:

%article.project
  %header
    %h1.generated
      #some_code
    %h2
      #some_code

我的“_project_toolbar.html.haml”看起来像:

%section.toolbar
   #some_code

这最终出现在 html 中,类似于:

<article class="project">
  #some_code
</article>
<section class="toolbar">
  #some_code
</section>

我想要的是代替

<article class="project">
   #some_code
   <section class="toolbar">
       #some_code
   </section>
</article>

我知道可以解决一些问题,比如在“project_header”中渲染我的“project_toolbar”。但它们是两个逻辑上独立的实体,所以我不想那样做。 有什么建议吗?

【问题讨论】:

  • 除了在project_header 中渲染project_toolbar 之外,我看不到任何其他方式。
  • 你知道另一种方式可能是剥离“文章”,而写作就在两者呈现之前。不过,如果它是 erb,它本来是可能的.. 不是 haml 比 erb 更好吗?

标签: html ruby-on-rails ruby-on-rails-4 haml renderpartial


【解决方案1】:

在haml中是可能的,一种方法是

%article.project
  = render 'project_header', :locals => {@set_tab => "current"}
  = render 'project_toolbar'

或者其他方式是,在_project_header.html.haml,渲染_project_toolbar.html.haml

%article.project
  %header
    %h1.generated
      #some_code
    %h2
      #some_code
  = render 'project_toolbar'

【讨论】:

  • 我想我想问的是,在 erb 中做这件事相当于什么?在 erb 中,我们可以在“project_header”中打开文章标签,然后在我们想要的任何地方关闭它。
  • haml 自行关闭所有标签,所以我不确定是否可以在 erb 中做你想做的事情,但我认为这个答案无论如何都更干净。
猜你喜欢
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-31
  • 2011-05-05
  • 1970-01-01
相关资源
最近更新 更多