I am trying to wrap my brain around what the best is for using an Ajax request and appending it to the dom.
So far I have tried the following:
- with jQuery I made a function 'renderFields' - that handles the data
- in the controller I render a twig view and 'return this'.
Both have their challenges. Let me start with the twig setup:
- because I only need to load a partial of the page I create a seperate twig file for the piece of html that I would like to render. This will increase the amount of twig files.
- Even dough I see the data (html) is sending back (console.log) - I still get an error in stead of success.
The jQuery cons:
- In the bottom of the twig file I add this 'renderFields' method. This is different from page to page - due to the needs. This will increase the amount of data (server-side).
- It is hard to make nested statements in a 'append()' function. For example I can't figure out how to loop trough results in the following:
for(i=0; i<items.length; i++)
<h5>'+ item[i].title + '</h5>\
<span>'+ item[i].message + '</span>'+
(item[i].user === '' ? '' : '<span>'+ item[i].user + '</span>') +
for (i=0; i<items.nodes; i++)
'<p>' + item.nodes[i] + '</p>'
In this example the for loop won't work.
Any other options, tips or solutions are also welcome (I am still a very beginner aka noob ).