Jump to content

Ajax with markup regions


rushy
 Share

Recommended Posts

Hello. I have recently adopted PW markup regions and really like this way of working. However, I am also trying to learn how to use Ajax and I am not sure of a good way to use the two together. Has anyone got any experience, tips or hints on using them together? For ajax -  I've used a simple scheme where I have a "webservice" template and page that handles Ajax requests and returns the appropriate content wrapped with some markup for the requesting page. I have markup regions enabled and all my pages (bar webservice) include a _main.php which brings in the headers, a default body and the footer. My javascript intercepts the page links and does my ajax call to webservice and that sends back the appropriate markup which is then placed by in the div #body defined in _main.php.  Does this seem a reasonable way to work? I guess I am looking for some advice before I invest too much time going the wrong way!

Any guidance, remarks,  comments, or a nudge in the right direction greatly appreciated.

Paul

Link to comment
Share on other sites

So far I found that using if(!$config->ajax): in my _main.php around head and foot parts and having a div targetted by Javascript as is commonly used - but in the case of an Ajax call I use wireRenderFile() to load markup from a file containing just the markup I need seems to work.... but I'm still playing with this. 

Paul

  • Like 1
Link to comment
Share on other sites

On 3/23/2019 at 9:24 AM, rushy said:

So far I found that using if(!$config->ajax): in my _main.php around head and foot parts and having a div targetted by Javascript as is commonly used - but in the case of an Ajax call I use wireRenderFile() to load markup from a file containing just the markup I need seems to work.... but I'm still playing with this. 

For not so complex ajaxified pages this is what I do. Something like:

<?php
if ($config->ajax) {
	echo wireRenderFile("./rendered-ajax-call-result.php", array('page' => $page));
	return $this->halt();
}

Page is the only variable passed to the template partial and if something else is needed I make sure it is already part of page so that I can access it in the partial. The halt method is the recommended way so that ProcessWire can tidy up before exiting the process.

Edited by szabesz
typo
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...