Jump to content

Recommended Posts

Posted

Hello all, is there a way to create a totally blank template? I need to house an ajax processor which returns a result, but with regular templates it gives me the whole page as the return value which kaboshes the response message.

I need to have the ajax processor as a site page so that it has access to to specific variables before processing.

Any guidance is appreciated.

 

Posted

Which regular templates do you mean?

A new template contains only the title field and doesn't return anything until you create a template file and tell it to return (echo) something.

  • Like 1
Posted (edited)

Hello,

Before outputting anything in your template file, simply check: if ($config->ajax)...

eg:

If you do not use jQuery – which does this for you – you need to add this key-value-pair to your requests:

key: X-Requested-With
value: XMLHttpRequest

like :

request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
Edited by szabesz
EDIT: added clarification to first sentence
  • Like 2
Posted
5 hours ago, breezer said:

but with regular templates it gives me the whole page as the return value which kaboshes the response message.

It's okay to use a page/template for an AJAX response, but if you're using a delayed output approach with an auto-appended main.php file then remember to disable that for your AJAX template: Edit Template > Files > Disable automatic append of file...

 

  • Like 2
  • Thanks 1
Posted

Thanks Robin S, your reply was exactly what I needed in this particular use case ?

And to everyone else who replied many thanks as well. I now realize my question was kinda vague, next time I'll try and be a bit more descriptive.

  • Like 2
Posted

Another option is using this in your template:

<?php
echo "my ajax response";
$this->halt();

That's similar to robin's suggestion but you can also use it without creating a separate file/template only for the ajax requests. For example you can send the ajax request to the page itself (the current url) and check if it is an ajax request or not. If it is an ajax request, you echo only the response, if not, you render the page (with the automatic appended main markup file).

<?php
if($config->ajax) {
  echo "my response";
  $this->halt();
}
?>
<region id="main">
  <h1>my page markup</h1>
  <p>blablabla</p>
</region>

$this->halt() is better than die() because it will fire after render hooks for example and just stop rendering the template output while die() will stop all further executions.

  • Like 1
Posted

This is what the final working template file looks like:
 

<?php  namespace ProcessWire;

/*FileCompiler=0*/

defined( 'XFORUM_MODE' ) or die( '<b>UNAUTHORIZED_ACCESS_ERROR</b>' );

// check for valid_key and compare to users id
if( wire('input')->get->text('valid_key') == wire('session')->user('id') ){
	echo xforum_process_page( $pname='ajax', $return='echo', $options, $xarray = $xforum);
}else{
	echo "error";
}
// Call halt() to prevent further loading
$this->halt();

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...