如何打印每个主题的评论? [英] How to print comments for every topic?
问题描述
/ **
* @Route(/主题/ {id},name =topic_details)
* @param $ id
* @return \Symfony\Component\HttpFoundation\Response
* /
public function topicDetailsAction($ id)
{
$ topic = $ this-> getDoctrine() - > getRepository('AppBundle:Topic') - > find($ id);
return $ this-> render('topics / topic.details.html.twig',array(
'topic'=> $ topic
));
}
现在尝试使用此功能显示当前所选主题的注释CommentController:
/ **
* @Route(/ topic / {id},name = topic_details)
* @return \Symfony\Component\HttpFoundation\Response
* /
public function listCommentsAction($ id)
{
$ topic = $ this-> getDoctrine() - > getRepository('AppBundle:Topic') - > find($ id);
$ topicComments = $ topic-> getComments();
return $ this-> render('topics / topic.details.html.twig',array(
'topicComments'=> $ topicComments
));
}
毕竟,当我尝试打印所有的数据在twig中的具体主题,我得到以下异常:
变量topicComments不存在。
我确定问题不大,可以解决,但不知道我错过了什么。
这是我的树枝模板:
{%extends'base.html.twig '%}
{%block body%}
< div class =container>
< div class =panel panel-primary>
< div class =panel-heading>
< h3 class =panel-title>< a href =/ topic / {{topic.id}}> {{topic.title}}< / a> H3>
< / div>
< br>
< div class =container>
< h3>说明:< / h3>
< div class =well>
< div class =panel-body>
{{topic.description}}
< / div>
< / div>
< / div>
< hr>
< div class =panel-body>
< div class =well>
< b>
类别:{{topic.category}}< br>
创建:{{topic.dateCreated | date(m / d / Y H:i:s)}}
< / b>
< / div>
< / div>
< / div>
< div class =container>
< div class =panel panel-primary>
< div class =panel-body>
< / div>
< / div>
< / div>
< a href =/ {{topic.id}} / comment / addclass =btn btn-lg btn-default>发表评论< / a>
< / div>
< div class =container>
{%在topicComments中评论%}
{{comment.description}}
{%endfor%}
< / div>
{%endblock%}
继续传递主题变量实例并导航树枝中的关系,因此渲染控制器:
return $ this-> render ('topics / topic.details.html.twig',array(
'topic'=> $ topic
));
然后在模板中:
{%在topic.comments中的评论%}
/ pre>
{{comment.description}}
{%endfor%}
I have 2 tables topics and comments: In the comments table a have column named topic_id where the id is the number, which corresponds to the commented topic from the user. I list the topic details with the following function in the TopicController:
/** * @Route("/topic/{id}", name="topic_details") * @param $id * @return \Symfony\Component\HttpFoundation\Response */ public function topicDetailsAction($id) { $topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id); return $this->render('topics/topic.details.html.twig', array( 'topic' => $topic )); }
Now im trying to display the comments for the current selected topic with this function in the CommentController:
/** * @Route("/topic/{id}", name="topic_details") * @return \Symfony\Component\HttpFoundation\Response */ public function listCommentsAction($id) { $topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id); $topicComments = $topic->getComments(); return $this->render('topics/topic.details.html.twig', array( 'topicComments' => $topicComments )); }
After all, when i try to print all the data from specific topic in the twig, i got the following exception:
Variable "topicComments" does not exist. Im sure the problem isn't big and can be solved, but not sure what i'm missing. Here's my twig template:
{% extends 'base.html.twig' %} {% block body %} <div class="container"> <div class="panel panel-primary"> <div class="panel-heading"> <h3 class="panel-title"><a href="/topic/{{ topic.id }}">{{ topic.title }}</a></h3> </div> <br> <div class="container"> <h3>Description:</h3> <div class="well"> <div class="panel-body"> {{ topic.description }} </div> </div> </div> <hr> <div class="panel-body"> <div class="well"> <b> Category: {{ topic.category }} <br> Created: {{ topic.dateCreated|date("m/d/Y H:i:s") }} </b> </div> </div> </div> <div class="container"> <div class="panel panel-primary"> <div class="panel-body"> </div> </div> </div> <a href="/{{ topic.id }}/comment/add" class="btn btn-lg btn-default">Leave a Comment</a> </div> <div class="container"> {% for comment in topicComments %} {{ comment.description }} {% endfor %} </div> {% endblock %}
解决方案You could continue passing the topic variable instance and navigate the relation in the twig, so render the controller:
return $this->render('topics/topic.details.html.twig', array( 'topic' => $topic ));
Then in the template:
{% for comment in topic.comments %} {{ comment.description }} {% endfor %}
这篇关于如何打印每个主题的评论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!