【问题标题】:Nesting html tags in haml在haml中嵌套html标签
【发布时间】:2014-02-06 14:02:37
【问题描述】:

在我的 Rails 应用程序中嵌套 html/haml 标签时遇到问题。我有应用的默认布局:

!!!
%html
  %head
    = render 'shared/head'
  %body
    = render 'shared/alerts'
    = render 'shared/header'
    .content
      = yield
    = render 'shared/footer'

我有这个 html:

<html>
  <head>..</head>
  <body>
    <div class="menubar">..</div>
    <div class="content">..</div>
    <div class="footer">..</div>
  </body>
</html>

如您所见,footer 不在.content 范围内。问题是footer 在一个视图文件中嵌套到.content,我不知道为什么:

    <html>
      <head>..</head>
      <body>
        <div class="menubar">..</div>
        <div class="content">
          <div class="footer">..</div>
        </div>
      </body>
    </html>

查看 html 被破坏的文件

<div id='topic' class='#{'un' unless @topic.locked?}locked'>
\#{render :partial => 'forem/topics/head', :locals => { :topic => @topic }}

.small-offset.up
  - if @topic.can_be_replied_to? && can?(:reply, @topic)
    = link_to t(".reply"), forem.new_topic_post_path(@topic), class: "button medium rounded lime"
  - if @topic.user == forem_user || forem_admin?
    = link_to t(".delete"), forem.forum_topic_path(@forum, @topic), method: :delete, data: { confirm: t("are_you_sure") }
  - if forem_user
    - if !@topic.subscriber?(forem_user.id)
      = link_to t(".subscribe"), forem.subscribe_forum_topic_path(@forum, @topic), class: "button medium rounded blue"
    - else
      = link_to t(".unsubscribe"), forem.unsubscribe_forum_topic_path(@forum, @topic), class: "button medium rounded pink"
  - if forem_admin?
    = link_to t('forem.topic.links.edit'), forem.edit_admin_topic_path(@topic)
    = link_to t(".hide.#{@topic.hidden}"), forem.toggle_hide_admin_topic_path(@topic), method: :put
    = link_to t(".lock.#{@topic.locked}"), forem.toggle_lock_admin_topic_path(@topic), method: :put
    = link_to t(".pin.#{@topic.pinned}"), forem.toggle_pin_admin_topic_path(@topic), method: :put
  - if @topic.pending_review?
    = t(".pending_review")
    - if forem_admin_or_moderator?(@topic.forum)
      = form_for @topic, url: forem.moderate_forum_topic_path(@topic.forum, @topic), method: :put do |f|
        = render "/forem/moderation/options", f: f
  = forem_pages_widget(@posts)
  = render partial: "forem/posts/post", collection: @posts
  = forem_pages_widget(@posts)

【问题讨论】:

  • 我们也没有,因为我们看不到有问题的文件。我怀疑是时髦的 HTML,特别是如果这是你在 DOM 中看到的——浏览器会尝试修复损坏的 HTML。
  • 我对haml了解不多,但我知道缩进会有所作为。您是否尝试过像 yield 语句一样缩进 =render 'shared/footer' 语句?
  • @DaveNewton 我添加了视图文件。我认为可能不清楚,但这正是 HTML 正在破坏的视图文件
  • @PhillipKregg 这将重现 OP doesnt' 想要的嵌套 div。
  • @DaveNewton 我一定误解了这个问题 - 我认为他确实希望将页脚嵌套在当时未包含的其他文件中。

标签: html ruby-on-rails haml


【解决方案1】:

它中断的视图缺少一个结束 div 标签,你打开 div:

&lt;div id='topic' class='#{'un' unless @topic.locked?}locked'&gt;

但不要关闭它,尝试在末尾添加&lt;/div&gt;,这应该可以解决它

或者,最好将 div 转换为 haml,然后缩进会自行解决:

#topic{class: @topic.locked? "locked" : "unlocked"}
  = render :partial => 'forem/topics/head', :locals => { :topic => @topic }

  .small-offset.up
    - if @topic.can_be_replied_to? && can?(:reply, @topic)
      = link_to t(".reply"), forem.new_topic_post_path(@topic), class: "button medium rounded lime"
    - if @topic.user == forem_user || forem_admin?
      = link_to t(".delete"), forem.forum_topic_path(@forum, @topic), method: :delete, data: { confirm: t("are_you_sure") }
    - if forem_user
      - if !@topic.subscriber?(forem_user.id)
        = link_to t(".subscribe"), forem.subscribe_forum_topic_path(@forum, @topic), class: "button medium rounded blue"
      - else
        = link_to t(".unsubscribe"), forem.unsubscribe_forum_topic_path(@forum, @topic), class: "button medium rounded pink"
    - if forem_admin?
      = link_to t('forem.topic.links.edit'), forem.edit_admin_topic_path(@topic)
      = link_to t(".hide.#{@topic.hidden}"), forem.toggle_hide_admin_topic_path(@topic), method: :put
      = link_to t(".lock.#{@topic.locked}"), forem.toggle_lock_admin_topic_path(@topic), method: :put
      = link_to t(".pin.#{@topic.pinned}"), forem.toggle_pin_admin_topic_path(@topic), method: :put
    - if @topic.pending_review?
      = t(".pending_review")
      - if forem_admin_or_moderator?(@topic.forum)
        = form_for @topic, url: forem.moderate_forum_topic_path(@topic.forum, @topic), method: :put do |f|
          = render "/forem/moderation/options", f: f
    = forem_pages_widget(@posts)
    = render partial: "forem/posts/post", collection: @posts
    = forem_pages_widget(@posts)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多