문제

I am wanting to create a templateData helper function to create widget like functionality. I have successfully created a widget for generate html code for related posts but am battling to create helper for creating prior posts widget.

My helper functions firsts calls getCollection('posts') to create list of posts and then tries to step though each item build a sorted list - much like the bootstrap skeleton code does on pages/posts.html.eco page. My problem is that on build by sorted list I get an inserted comma ',' see html extracted from rendered page:

<nav class="linklist">
  <ul>
    <li><a href="/posts/bye">A Goodbye Blog Post</a></li>,
  </ul>                 
</nav>

My helper function defined in docpad.coffee under templateData is:

getPriorPostsWidget: (widgetName) ->      
  if @getCollection('posts').length
    pPosts = @getCollection('posts').toJSON()
    myInner = for priorPost in pPosts
      if @document.url != priorPost.url
        """
        <li>
          <a href="#{priorPost.url}">#{priorPost.title}</a>
        </li>
        """

    return """
      <aside id="prior-posts">
        <h3 class="widget-title">#{widgetName}</h3>
        <nav class="linklist">
          <ul>
            #{myInner}
          </ul>                 
        </nav>
     </aside>
     """        

and finally is inserted into .eco layout file as

<%- @getPriorPostsWidget("Prior Posts") %> 

Code has gotten a tad untidy trying to debug but I have now run out of ideas and thought I'd reach out for help as I am obviously missing some salient point.

Update: Okay, it must have been a really long day, my error is rather silly. What I was assuming was a string is an array of strings and the default handling of array.tostring is to insert a ','. Changing #{myInner} to #{myInner.join(''} solves the problem!

Colin

도움이 되었습니까?

해결책

Problem is that myInner is an array and not a string. The default handling of Arrays -> String is to add the ',' character. Solution is to join array with empty space using join('').

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