문제

HAML을 사용하여 간단한 중첩 HTML 메뉴를 만들려고 노력하고 있으며 요소를 올바른 들여 쓰기, 또는 중첩 된 나무를 짓는 가장 좋은 방법. 나는 이런 일을하고 싶지만 무한히 깊이 :

- categories.each_key do |category|
    %li.cat-item{:id => "category-#{category}"}
        %a{:href => "/category/#{category}", :title => "#{category.titleize}"}
            = category.titleize

HTML에서 태그를 손으로 쓰지 않고도 쉽게이를 쉽게 달성 할 수 있어야한다고 생각하지만 재귀는 최고는 아닙니다. 현재 제가 생각해 낸 코드는 다음과 같습니다.

도우미를보십시오

def menu_tag_builder(array, &block)
  return "" if array.nil?
  result = "<ul>\n"
  array.each do |node|
    result += "<li"
    attributes = {}
    if block_given?
      text = yield(attributes, node)
    else
      text = node["title"]
    end
    attributes.each { |k,v| result += " #{k.to_s}='#{v.to_s}'"}
    result += ">\n"
    result += text
    result += menu_tag_builder(node["children"], &block)
    result += "</li>\n"
  end
  result += "</ul>"
  result
end

def menu_tag(array, &block)
  haml_concat(menu_tag_builder(array, &block))
end

보다

# index.haml, where config(:menu) converts the yaml below
# to an array of objects, where object[:children] is a nested array
- menu_tag(config(:menu)) do |attributes, node|
 - attributes[:class] = "one two"
 - node["title"]

Yaml 정의 메뉴 샘플

menu:
  -
    title: "Home"
    path: "/home"
  -
    title: "About Us"
    path: "/about"
    children: 
      -
        title: "Our Story"
        path: "/about/our-story"

이를 수행하는 방법이므로 출력은 다음과 같습니다.

<ul>
  <li class='one two'>
    Home
  </li>
  <li class='one two'>
    About Us
  </li>
</ul>

...이건 아니야:

<ul>
<li class='one two'>
Home</li>
<li class='one two'>
About Us</li>
</ul>

... 그래서 그것은 전 세계적으로 제대로 들여 쓰기입니다.

도와 주셔서 감사합니다, 랜스

도움이 되었습니까?

해결책

멋지게 문지르는 루비로 생성 된 HAML 코드에 대한 트릭은 다음과 같습니다. haml_tag 돕는 사람. 내가 당신을 변환하는 방법은 다음과 같습니다 menu_tag 사용 방법 haml_tag:

def menu_tag(array, &block)
  return unless array
  haml_tag :ul do
    array.each do |node|
      attributes = {}
      if block_given?
        text = yield(attributes, node)
      else
        text = node["title"]
      end
      haml_tag :li, text, attributes
      menu_tag_builder(node["children"], &block)
    end
  end
end

다른 팁

라인을 따라 무언가는 어떻습니까 :

def nested_list(list)
  return unless list
  haml_tag :ul do
    list.each do |item|
      haml_tag :li do
        haml_concat link_to item["title"], item["path"]
        if item["children"]
          nested_list item["children"]
        end
      end
    end
  end
end

굉장합니다. @Shingara의 힌트는 나를 올바른 방향으로 만들었습니다 :). 이것은 완벽하게 작동합니다 :

def menu_tag(array, &block)
  return "" if array.nil?
  haml_tag :ui do
    array.each do |node|
      attributes = {}
      if block_given?
        text = yield(attributes, node)
      else
        text = node[:title]
      end
      haml_tag :li, attributes do
        haml_concat text
        menu_tag_builder(node[:children], &block)
      end
    end
  end
end

누군가가 더 짧게 만들거나 중첩 노드에서 속성을 사용자 정의하는 것이 더 쉽게 만들 수 있다면,이 대신에 올바르게 표시 할 것입니다.

건배.

도우미가 Pur HTML을 보내기 때문입니다. 압입은 Haml과 함께됩니다. 당신은 당신의 도우미에 일부 Haml을 생성 할 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top