2015-07-13 55 views
0

我正在使用SilverStripe的博客模塊。在博客持有者模板中,有一個循環遍歷每個博客條目。我無法在這個循環中運行博客條目的任何方法,我只能運行博客持有者方法。所以..我想將當前博客條目的ID傳遞給我編寫的博客持有者方法,以便我可以從博客條目中檢索AuthorID(我添加的自定義字段)。將博客條目的ID傳遞給博客持有者方法

我已經試過

BlogHolder.php

function getAuthor($identity) { 
    $Author = DataObject::get_by_id('Person', $identity); 
} 

BlogHolder.ss

<p id="author-tag">By $getAuthor($ID)</p> 

我也試圖通過用於測試目的的直接整數,這也返回一個有關未定義的$ identity變量的錯誤,所以它似乎沒有任何交流根本沒有被傳遞給該方法。

我收到的錯誤是

[警告]缺少參數1 BlogHolder_Controller :: getAuthor(),稱爲/var/www/sites/quadra/framework/view/ViewableData.php上線108和定義

以及

[注意]未定義變量:身份

在博客中的博客條目循環:

也..

[用戶警告]數據對象:: get_by_id通過了非數字ID #DataList

因此,這裏真正的問題是持有人模板,我如何將當前博客條目的ID傳遞給博客持有者控制器中的方法。

謝謝你們。

回答

1

我猜你正在使用的SilverStripe Blog module 1.0 branch爲SilverStripe 3.1。

我無法在此循環中從博客條目運行任何方法,我只能運行博客持有者方法。

您應該可以調用BlogEntry類中的所有BlogEntry變量和函數。您不能從BlogHolder頁面調用BlogEntry_Controller函數,但可以調用所有BlogEntry變量和函數。

BlogEntry有一個Author文本變量在$db所以你應該可以在你的博客條目循環中調用$Author

您可以使用博客模塊附帶的模板。這將顯示信息,如條目的作者,標籤,摘要,日期。

或者,如果您要創建自己的自定義模板,則可以查看現有模板作爲參考。

這是當前1.0分支博客持有者模板以及它如何通過條目循環。

BlogHolder.ss

<% include BlogSideBar %> 

<div id="BlogContent" class="blogcontent typography"> 

    <% include BreadCrumbs %> 

    <% if SelectedTag %> 
     <h3><% _t('BlogHolder_ss.VIEWINGTAGGED', 'Viewing entries tagged with') %> '$SelectedTag'</h3> 
    <% else_if SelectedDate %> 
     <h3><% _t('BlogHolder_ss.VIEWINGPOSTEDIN', 'Viewing entries posted in') %> $SelectedNiceDate</h3> 
    <% else_if SelectedAuthor %> 
     <h3><% _t('BlogHolder_ss.VIEWINGPOSTEDBY', 'Viewing entries posted by') %> $SelectedAuthor</h3> 
    <% end_if %> 

    <% if BlogEntries %> 
     <% loop BlogEntries %> 
      <% include BlogSummary %> 
     <% end_loop %> 
    <% else %> 
     <h2><% _t('BlogHolder_ss.NOENTRIES', 'There are no blog entries') %></h2> 
    <% end_if %> 

    <% include BlogPagination %> 

</div> 

在BlogEntries循環,它包括爲每個條目模板BlogSummary.ss。

BlogSummary.ss

<div class="blogSummary"> 
    <h2 class="postTitle"><a href="$Link" title="<% _t('BlogSummary_ss.VIEWFULL', 'View full post titled -') %> '$Title'">$MenuTitle</a></h2> 
    <p class="authorDate"><% _t('BlogSummary_ss.POSTEDBY', 'Posted by') %> $Author.XML <% _t('BlogSummary_ss.POSTEDON', 'on') %> $Date.Long | <a href="$Link#comments-holder" title="View Comments Posted">$Comments.Count <% _t('BlogEntry_ss.COMMENTS', 'Comments') %></a></p> 
    <% if TagsCollection %> 
     <p class="tags"> 
      <% _t('BlogSummary_ss.TAGS','Tags') %>: 
      <% loop TagsCollection %> 
       <a href="$Link" title="View all posts tagged '$Tag'" rel="tag">$Tag</a><% if not Last %>,<% end_if %> 
      <% end_loop %> 
     </p> 
    <% end_if %> 

    <% if BlogHolder.ShowFullEntry %> 
     $Content 
    <% else %> 
     <p>$Content.FirstParagraph(html)</p> 
    <% end_if %> 

    <p class="blogVitals"> 
     <a href="$Link#comments-holder" class="comments" title="View Comments for this post"> 
      $Comments.Count <% _t('BlogSummary_ss.SUMMARYCOMMENTS','comment(s)') %> 
     </a> 
     | 
     <a href="$Link" class="readmore" title="Read Full Post"> 
      <% _t('BlogSummary_ss.READFULLPOST','Read the full post') %> 
     </a> 
    </p> 
</div> 

在BlogSummary你可以看到像$作者和$ Date.Long項目。

像這樣創建您的模板。您不需要在傳入條目ID的BlogHolder中調用一個函數。

0

假設有來自BlogEntry一個HAS_ONE關係,作者,你應該能夠把<p id="author-tag">$Author.Name</p>在模板