Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [jQuery][symfony] czy ajax musi dostać proste dane np json czy może dostać sparsowany twig ze zmiennymi, pętlami?
porzeczki
post
Post #1





Grupa: Zarejestrowani
Postów: 144
Pomógł: 0
Dołączył: 15.09.2016
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


czy mój kontroler wywoływany przez ajax zamiast zwracać jakiś string:
  1. return new JsonResponse(array('data' => $data));

może zwrócić :
  1. if ($request->isXmlHttpRequest()) {
  2. ...
  3. return $this->render('AppBundle:Default:index.html.twig',[
  4. 'foo' => $foo
  5. ]);

z twigiem:
  1. <a href={{ path('show', { 'id': foo }) }}>


?

  1. $.ajax({
  2. type: "GET",
  3. url: Routing.generate('index')
  4. })
  5. .done(function( data ) {
  6. console.log(data);
  7. });
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
mcmaroon
post
Post #2





Grupa: Zarejestrowani
Postów: 11
Pomógł: 5
Dołączył: 30.11.2016

Ostrzeżenie: (0%)
-----


Tak, np coś w tym stylu
  1. $renderData = [];
  2. $renderData['status'] = true;
  3. $renderData['count'] = 10;
  4. $renderData['template'] = $this->renderView('AppBundle:Default:index.html.twig', array(
  5. 'var' => $var
  6. ));
  7.  
  8. return $this->json($renderData);

i potem odbierasz np
Kod
console.log(data.status);
console.log(data.template);
$('#jakis-selektor).html(data.template);


Ten post edytował mcmaroon 3.03.2017, 22:37:03
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 14.10.2025 - 18:19