タイトルが正確かどうかわからないので、ここで説明するように頑張ります。
私がやろうとしているのは、Ajaxを使用してコントローラーを呼び出し、リロードせずにページのコンテンツを変更することです(コンテンツはブロックによって定義されています)。したがって、大まかな例として、ホームページの到着時のホームページのコンテンツは次のようになります。
# routing.yml
StackQuestionBundle_osgs_home:
pattern: /
defaults: { _controller: StackQuestionBundle:Pages:index }
options:
expose: true
StackQuestionBundle_osgs_page:
pattern: /page
defaults: { _controller: StackQuestionBundle:Pages:page }
options:
expose: true
<!-- @StackQuestionBundle:Pages:index.html.twig -->
<header>
{% block title %} <!-- Title --> {% endblock %}
<nav> </nav>
</header>
<main>
<aside> <!-- Sidebar --> </aside>
<section>
{% block content %} <!-- Main Content -->{% endblock %}
</section>
</main>
<footer>
{% block footer %} <!-- content --> {% endblock %}
<nav> </nav>
</footer>
// JS
$('nav button.test').click(function() {
$.ajax({
url: "{{ path('OSGSBundle_osgs_page') }}",
type: "POST",
success: function(data) {
//Change the content in the blocks!
//alert("It worked!")
}
});
});
# PagesController.php
namespace MyNameSpace\StackQuestionBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class PagesController extends Controller
{
public function indexAction()
{
return $this->render('OSGSBundle:Pages:index.html.twig');
}
public function pageAction(Request $request)
{
$isAjax = $this->get('Request')->isXMLHttpRequest();
if ($isAjax)
{
// Replace/Append the content in index.html.twig with the content from page.html.twig
}
else
{
// Load page normally if navigated to http://localhost/page
return $this->render('OSGSBundle:Pages:page.html.twig');
}
}
}
これは私の現在のコードがどのように見えるかです。これまでのところ、DOM要素のみに従ってデータを置き換えることができました。<main>
たとえば、タグ内のすべての情報を置き換えます。Twigブロックタグのコンテンツのみを置き換えたい。補足:私はFOSJSRoutingBundleを使用してURLを変更しています。
よろしくお願いします。ヘルプ/アドバイスに感謝します。これがあまり混乱していなかったと思います。