Symfony2-Twig:仅渲染模板的单个部分

时间:2013-12-07 22:06:33

标签: php symfony twig

我有一个主页,动态加载一堆“帖子”(类似于Facebook墙)。

可以选择关注帖子,但是当有人点击跟随按钮时,我会需要重新加载所有主页以更新此值。如何仅更新与帖子对应的部分?实际上帖子是在extern hmtl.twig文件中,我从主页代码渲染:

<div class="row">
        <div class="col-md-6">
         {% for sueno in suenos %}
            {% if loop.index0 is even %}
                {{ render(controller('SuenoBundle:Default:suenoFrontend', { 'sueno': sueno } )) }}
            {% endif %}
         {% endfor %}
       </div><!--/col-md-6-->

这是控制器:

public function suenoFrontendAction($sueno)
{
        return $this->render('SuenoBundle:Default:suenoFrontend.html.twig', array(
        'sueno' => $sueno));
}

这是post html文件:

                <h4 class="text-center nombre-usuario">{{ sueno.usuario.nombre ~ ' ' ~ sueno.usuario.apellido }}</h4>   
                  {% if sueno.necesitaAyuda == 1 %}
                    <p class="text-center texto-sueno"><img src="{{ asset('bundles/estructura/images/SOS.png') }}" width="25" height="25" alt="SOS" /></p>       
                  {% endif %}                    
                  {% if sueno.rol == 'foto' %}
                          <a href="#" data-toggle="modal" data-target="#imageModal" data-image="{{ asset('upload/images/' ~ sueno.fotoLink) }}" onclick="verFoto(this.getAttribute('data-image'))"><img src="{{ asset('upload/images/' ~ sueno.fotoLink) }}" class="img-responsive img-thumbnail" alt="Responsive image"></a>
                  {% elseif sueno.rol == 'video' %}
                          <div class="video-container"><iframe src="{{ sueno.linkVideo }}" frameborder="0" allowfullscreen=""></iframe></div>
                  {% endif %}
                          <h4 class="text-center titulo-sueno">{{ sueno.titulo }}</h4>
                          <h4 class="text-center quepido-sueno">{{ sueno.quePido }}</h4>
                          <p class="text-center texto-sueno info">{{ sueno.texto }}</p>   
                          <br>
                        <div>
                            {% set size = sueno.usuariosColaboradores | length %}
                            {% set size2 = sueno.usuariosSeguidores | length %}
                            <p class="text-center opciones-sueno">
                            <img src="{{ asset('bundles/estructura/images/colaboradores.png') }}" width="20" height="20" alt="Colaboradores" /> {{ size }}
                            <img src="{{ asset('bundles/estructura/images/punto.png') }}" width="5" height="5" alt="punto" />
                            <a href="{{ path('seguir', { 'suenoid': sueno.id }) }}"><img src="{{ asset('bundles/estructura/images/seguidores.png') }}" width="20" height="20" alt="Seguidores" /></a> {{ size2 }}
                            <img src="{{ asset('bundles/estructura/images/punto.png') }}" width="5" height="5" alt="punto" />
                            <img src="{{ asset('bundles/estructura/images/compartir.png') }}" width="20" height="20" alt="Compartir" />
                            </p>
                        </div>
                    <hr>

在这里你可以看到跟踪帖子的相关动作(在前面的代码中,我注意到了这一行):

<a href="{{ path('seguir', { 'suenoid': sueno.id }) }}"><img src="{{ asset('bundles/estructura/images/seguidores.png') }}" width="20" height="20" alt="Seguidores" /></a> {{ size2 }}

size2值是要更新的值。单击此处时调用的控制器是这个(我认为失败的地方):

    public function seguirAction($suenoid)
{

    $em = $this->getDoctrine()->getManager();

    $usuario = $this->getUser();

    $sueno = $em->getRepository('SuenoBundle:Sueno')->findOneBy(array('id' => $suenoid));

    $usuario->getSuenosSigue()->add($sueno);  
    $em->persist($usuario);
    $em->flush();

  // $suenos = $em->getRepository('SuenoBundle:Sueno')->findBy(array('usuario' => $usuario->getId()));

    //esto hay que cambiarlo entero para conseguir los sueños apropiados
        //return $this->render('EstructuraBundle:Home:home.html.twig', array('suenos'=> $suenos , 'usuario' => $usuario));
                return $this->render('SuenoBundle:Default:suenoFrontend.html.twig', array(
        'sueno' => $sueno));
}

事情是,当这个渲染发生时,帖子会填满整个页面。

我想要的只是在主页上重新发布这篇文章

0 个答案:

没有答案