【问题标题】:how to refresh block in twig如何刷新树枝中的块
【发布时间】:2014-12-08 14:01:09
【问题描述】:

每 5 秒调用一次控制器的 JavaScript 代码

                           $.post('{{path('nb_invitation')}}',
                             {data: '1'},
                             function(response){
                                 if(response.code == 100 && response.success){
                                     alert('yes');
                                 }
                             }, "json");   

代码:

我的通知Iv.html

{% block notificationIv -%}  
    // content
    {% endblock %}

生根

 nb_invitation:
        path:     /communaute/nb_invitation
        defaults: { _controller: communauterBundle:notificationIv:nb_invitation }

控制器

public function nb_invitationAction(Request $request){
     return  $this->render('communauterBundle:notificationIv:notificationIv.html.twig');
    }

index.html

  {% block notificationIv -%}
         {{ render(controller('communauterBundle:notificationIv:nb_invitation')) }}
    {% endblock %}

【问题讨论】:

  • 我看不到我的块中的变化 块没有得到新数据
  • 这个 JavaScript 代码是在外部 .js(包含)还是内联在

标签: javascript php symfony twig


【解决方案1】:
$.post('{{path('nb_invitation')}}',
   {data: '1'},
   function(response){
          if(response.code == 100 && response.success){
              $("#notification_identifier").html(response.data)     // or whatever you return 
          }
}, "json"); 

正如弗洛伦所说,你很困惑,上面的代码是一个使用jquery的例子,只需要定义一个id(或一个类)到通知容器,这样你就可以更新内容了..有些像

{% block notificationIv -%}  
  <div id="notification_identifier"></div>
{% endblock %}

【讨论】:

    【解决方案2】:

    我认为您对 JS 和 PHP 的使用感到困惑——它们在 html 页面中是不等价的。你的

    {{ render(controller('communauterBundle:notificationIv:nb_invitation')) }}
    

    不会因为您对控制器进行 ajax 调用而自行“刷新”。如果你真的想刷新,那么可能是

    index.html

    <div id="alert"></div>
    

    控制器

    return JsonResponse(array('message' => $message ))
    

    我不熟悉 jQuery ajax 语法,但基本上你制作了一个 GET ajax 并用你的 ajax 返回的任何内容替换 div#alert 中的 html 值。类似的东西(纠正我的语法)

    script.js

    $.ajax({
      url: "{{path('nb_invitation')}}",//even though you can use js routing in symfony, much cleaner
    }).done(function(response) {
        $.getElementById('alert').html(response.message)
    });
    

    我知道语法不准确,但这就是想法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-06
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多