Jump to content
breezer

How to create totally blank template

Recommended Posts

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.

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Typically for ajax calls I host a PW bootstrapped php file at the root of the site (ie above /site/).

  • Like 1

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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();

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...