2

I've started to digging into the Docpad for my current project and so far I've discovered 3 ways to include content.

  • We use content template data variable to include child layout/document into the layout.
  • We have include(relativePath) method to include the content of another file at the given path.
  • Finally we have partials plugin which provides the way to insert documents into other documents, and also be passed by the docpad rendering engine.

We can use content variable only once per document. But often we have multiple places within static template where we need to include content. So why we ever need to use content variable if we always can use, say, include, or partials (when installed) which does not have this limit of only single usage per document? I guess that there are own pros, cons and use cases for each way of including content. So I'm wondering which way is most appropriate and in what case, assuming that we want to include content multiple times in one layout?

4

1 回答 1

0

好的 - 我想我知道你想要做什么。页面上可能有不同的地方您可能想要包含内容。标准方法是在您想要内容的每个地方使用部分。另外,请记住,您也可以从部分中调用部分。这是我的博客页面的示例:

 <% for doc in @getCollection('posts').toJSON(): %>
       <%- @partial('content/post.html.eco',{document:doc,showComments:false}) %>
<div class="hr clearfix">&nbsp;</div>
 <% end %> 

在部分我有:

 <article class="panel">

    <header>
        <h3 class="title"><a href="<%= @document.url.replace(/\\/g, '/') %>"><%=@document.title%></a><time datetime="<%=@document.date.toISOString()%>" class="postdate"><%=@document.date.toDateString()%></time></h3>
    </header>

    <!-- Post Data -->
    <p class="sub"><strong class="tag_bg"></strong>
    <%for tag in @document.tags: %>
        <a class="catlink" href="/"><%=tag%></a> 
    <%end%>
    <a class="commentlink" href="#">0</a>
    </p>

    <% if (@document.image):%>
    <figure>

        <a class="islink_m" href="#" data-rel="prettyPhoto[mixed]" title="<%=@document.title%>"></a>
        <span class="image-holder-medium">
            <img class="thumb" style="display:block;margin-left:auto;margin-right:auto;" alt="" src="<%=@document.image%>"></span>

    </figure>
    <%end%>

    <p><%-@document.content%></p>

    <footer>
        <%if(@showComments):%>
    <%- @partial('content/social.html') %>
    <%- @partial('content/disqus.html.eco', {site:@site,document: @document}) %>
        <%end%>
    </footer>

</article>
于 2013-10-05T19:29:26.493 回答