php – AJAX调用如何使用TWIG
发布时间:2020-12-13 17:27:14 所属栏目:PHP教程 来源:网络整理
导读:我想了解Twig如何通过 AJAX载入模板. 从他们的网站,很明显如何加载一个模板(http://twig.sensiolabs.org/doc/api.html) echo $twig-render('index.html',array('the' = 'variables','go' = 'here')); 但是AJAX调用如何工作?你如何告诉Twig你想“渲染”一些
我想了解Twig如何通过
AJAX载入模板.
从他们的网站,很明显如何加载一个模板(http://twig.sensiolabs.org/doc/api.html) echo $twig->render('index.html',array('the' => 'variables','go' => 'here')); 但是AJAX调用如何工作?你如何告诉Twig你想“渲染”一些只是index.html的一部分,而不是重新加载整个页面?我查看了Twig的唯一Ajax示例(http://twig.sensiolabs.org/doc/recipes.html),但这并不能解释Twig如何知道您想要更改的页面的哪一部分.假设您的Ajax调用会产生页面内容更新.我只需要一个简单的例子,比Twig的食谱页面更重要.
有几种方法可以实现:
1)在index.html和content.html等文件中分离你的index.html. 示例: if(isAjaxRequest()) //try to find the right function here echo $twig->render('content.html','go' => 'here')) else echo $twig->render('index.html','go' => 'here')); 编辑: $.get('yoururl',function(data) { $('#divtoremplace').html(data); }); 2)在index.html中使用request.ajax boolean {% if request.ajax == false %} <p>My header,not reloaded with ajax</p> {% endif %} <p>My content,reloaded with ajax</p> {% if request.ajax == false %} <p>Other content,not reloaded with ajax</p> {% endif %} 不知道第二个,但是这应该是文档的诀窍.最好的方法是第一个解决方案,分离你的代码. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |