【问题标题】:Passing the ID of a blog entry to a blog holder method将博客条目的 ID 传递给博客持有者方法
【发布时间】:2015-07-13 13:28:56
【问题描述】:

我正在使用 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 变量未定义的错误,因此似乎实际上没有任何东西被传递给该方法。

我收到的错误是

[警告] BlogHolder_Controller::getAuthor() 缺少参数 1,在 /var/www/sites/quadra/framework/view/ViewableData.php 第 108 行调用并定义

还有

[注意] 未定义变量:identity

还有..

[用户警告] DataObject::get_by_id 传递了一个非数字 ID #DataList

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

谢谢大家。

【问题讨论】:

    标签: php silverstripe


    【解决方案1】:

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

    我无法在此循环中运行博客条目中的任何方法,我只能运行博客持有者方法。

    您应该能够调用BlogEntry 类中的所有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。

    【讨论】:

      【解决方案2】:

      假设从 BlogEntry 到 Author 存在 has_one 关系,您应该可以在模板中输入 &lt;p id="author-tag"&gt;$Author.Name&lt;/p&gt;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-07
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多