将博客条目的 ID 传递给博客所有者方法

Passing the ID of a blog entry to a blog holder method

我正在使用 SilverStripe 的博客模块。在博客持有人模板中有一个循环遍历每个博客条目。我不能 运行 此循环中博客条目的任何方法,我只能 运行 博客持有者方法。所以..我想将当前博客条目的 ID 传递给我编写的博客所有者方法,这样我就可以从博客条目中检索 AuthorID(我添加的自定义字段)。

我试过了

BlogHolder.php

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

BlogHolder.ss

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

我也曾尝试传递一个直接整数用于测试目的,这也返回了一个关于 $identity 变量未定义的错误,所以看起来实际上没有任何东西被传递给该方法。

我收到的错误是

[Warning] Missing argument 1 for BlogHolder_Controller::getAuthor(), called in /var/www/sites/quadra/framework/view/ViewableData.php on line 108 and defined

以及

[Notice] Undefined variable: identity

还有..

[User Warning] DataObject::get_by_id passed a non-numeric ID #DataList

所以这里的真正问题是:在博客持有者模板的博客条目循环中,如何将当前博客条目的 ID 传递给博客持有者控制器中的方法。

谢谢大家。

假设 BlogEntry 与作者之间存在 has_one 关系,您应该可以将 <p id="author-tag">$Author.Name</p> 放入模板中

我猜您正在使用 SilverStripe 3.1 的 SilverStripe Blog module 1.0 branch

I can't run any methods from blog entry in this loop, I can only run blog holder methods.

您应该能够调用 BlogEntry class 中的所有 BlogEntry 变量和函数。您不能从 BlogHolder 页面调用 BlogEntry_Controller 函数,但可以调用所有 BlogEntry 变量和函数。

BlogEntry$db 中有一个 Author 文本变量,因此您应该能够从博客条目循环内部调用 $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 中,您可以看到 $Author 和 $Date.Long.

等项目

像这样创建您的模板。您不需要调用 BlogHolder 中传递条目 ID 的函数。