FOSCommentBundle功能包:其它添加评论到页面的方法

本文介绍了FOSCommentBundle的默认实现如何通过异步javascript和jQuery加载评论线索,并详细解释了不使用javascript的替代方案及所需在Action和模板中进行的额外工作。包括在Action中插入必要的PHP代码,在模板中加入DOM树内的特定代码块,以及确保jQuery在页面可用,最终通过js文件引入进一步的交互。

Step 10: Other ways of including comments in a page

The default implementation of FOSCommentBundle uses asynchronous javascriptand jQuery (optionally with easyXDM for cross domain requests) to load a comment thread into a page.

FOSCommentBundle的缺省实现是使用异步javascript和jQuery(也可以使用easyXDM进行跨域请求)来将评论线索加载到页面。


It is possible to include the thread without using javascript to load it, but needs additional work inside the controller's action.

不使用javascript来引导线索加载也是可以实现的,但需要在控制器的Action中进行额外的工作。


At a minimum, you will need to include the following in your action's PHP code:

至少,您需要在您Action中的PHP代码中包含下列语句:

public function somethingAction(Request $request)
{
    $id = 'thread_id';
    $thread = $this->container->get('fos_comment.manager.thread')->findThreadById($id);
    if (null === $thread) {
        $thread = $this->container->get('fos_comment.manager.thread')->createThread();
        $thread->setId($id);
        $thread->setPermalink($request->getUri());
        // Add the thread
        $this->container->get('fos_comment.manager.thread')->saveThread($thread);
    }
    $comments = $this->container->get('fos_comment.manager.comment')->findCommentTreeByThread($thread);
    return $this->render('AcmeDemoBundle:Controller:something.html.twig', array(
        'comments' => $comments,
        'thread' => $thread,
    ));
}

Once you've included this code in your action, some code must be included in your template:

一旦您已经在您的Action中包含了上述代码,那么也必须在您的模板中包含一些代码:

{% block body %}
{# ... #}
<div id="fos_comment_thread" data-thread="` thread`.`id `">
{% include 'FOSCommentBundle:Thread:comments.html.twig' with {
    'comments': comments,
    'thread': thread
} %}
</div>
{# ... #}
{% endblock body %}
{% block javascript %}
{# jQuery must be available in the page by this time, and make sure javascript block is after
  <div id="fos_comment_thread"> in the DOM Tree, for example right before </body> tag
#}
{% javascripts '@FOSCommentBundle/Resources/assets/js/comments.js' %}
<script type="text/javascript" src="` asset_url `"></script>
{% endjavascripts %}
{% endblock javascript %}

That is it!

Return to the index.

返回到指南索引页


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值