我想了解 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 的食谱页面上的更多。
最佳答案
直接在模板中:
{% if app.request.isXmlHttpRequest() %}
// code if ajax request
{% else %}
// code if not ajax request
{% endif %}
关于php - AJAX 调用如何与 TWIG 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14154670/