We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Dynamically load partials

Would it be possible to dynamically load partials depending on the option a user selected in a drop-down list.

I am unfortunatly not working with Volt in my current project, so I would be looking for an ajax solution. I thought I could use jquery to catch the on change select , then make an ajax call to a getTemplateAction() method in my current controller, then call view->partial().

Anyone with a better suggestion would be appreciated.



43.9k

Hi,

Right, jQuery ajax facilities will help you, but in your case you should not use partials but pick view instead in the controller action that will respond to the ajax request (see https://docs.phalcon.io/en/latest/reference/views.html#picking-views)

and set render level to LEVEL_ACTION_VIEW (see https://docs.phalcon.io/en/latest/reference/views.html#control-rendering-levels).

Do the partials have a lot of logic in them? If they're pretty simple, you might consider including all possible partials at once, hide them with CSS, and show them with javascript when the user chooses the appropriate action.



43.9k

@quasipickle : elegant solution !

@quasipickle : I will have different sections, where I either want to load a textarea, file loader, or both. Around 4 sections. And for some, the user would be adding a number of parts that I wouldn't know in advance. So I can't really show/hide for that part. It would be like writing a paper, and wanting to add a new paragraph n number of times.

One method I used that was sort of similar was to load the partial as a parameter in the action. Of course you have to be careful about the naming of the partials and the ajax request and may have to do some work with render levels. So a request to /controller/action/parameter1 would return the parameter1.phtml partial.