문제

I'm using docpad and on index page, in navigation pane I want to get a list of links grouped by category.

Category is defined in each markdown document in meta info on top. For example category:"tutorials"

So I have this:

<% for docu in @getFilesAtPath("document-repository").toJSON(): %>
<li><h2><%=cat=docu.category%></h2></li>
   <%for docu in @getFilesAtPath("document-repository",category:cat}).toJSON():%>
       <li><a href="#<%=docu.url%>"><%=docu.title%></a></li>
   <%end%>
<% end %>

But of course it is bad as it is looping as many times as many documents I have. I have only one category and I want it to loop only once when list of links is printed.

With jekyll it was done like this (part of _includes nav.html from https://github.com/devo-ps/carte) :

{% for category in site.categories %}
  <li><h2>{{ category | first }}</h2>
    <ul>
    {% for posts in category %}
      {% for post in posts %}        
        <li class='{{ post.type }}'><a href='#{{ post.url }}'>{{ post.title }}</a></li>
      {% endfor %}
    {% endfor %}
    </ul>
  </li>
{% endfor %}

He somehow knows how many categories are there. I don't know how to port it to docpad

도움이 되었습니까?

해결책

I think the best question is when after asking in you find the answer :) So I found a "workaround" at least I think it is a workaround and not a solution. For me that is perfect:

  1. I've added "categories" to docpad.coffee file

        templateData:
                site:
                categories: ['Tutorials','General']
    

    Now I will always update this array with categories that should be used in meta info of each markdown doc

  2. My loop looks like this now....and works!!!

     <% for category in @site.categories : %>
        <li><h2><%- category  %></h2>
        <ul>
        <%for docu in @getFilesAtPath("document-repository",[{filename: -1}]).findAll({category:category}).toJSON():%>
                <li><a href="#<%=docu.url%>"><%=docu.title%></a></li>
        <% end %>
        </ul>
        </li>
    <% end %>
    
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top