breezer Posted November 21, 2018 Share Posted November 21, 2018 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. Link to comment Share on other sites More sharing options...
fbg13 Posted November 21, 2018 Share Posted November 21, 2018 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. 1 Link to comment Share on other sites More sharing options...
szabesz Posted November 21, 2018 Share Posted November 21, 2018 (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 November 21, 2018 by szabesz EDIT: added clarification to first sentence 2 Link to comment Share on other sites More sharing options...
adrian Posted November 21, 2018 Share Posted November 21, 2018 Typically for ajax calls I host a PW bootstrapped php file at the root of the site (ie above /site/). 1 Link to comment Share on other sites More sharing options...
breezer Posted November 21, 2018 Author Share Posted November 21, 2018 Thanks for the replies, I'll give the suggestions a try and let you know how it goes. Link to comment Share on other sites More sharing options...
Robin S Posted November 21, 2018 Share Posted November 21, 2018 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... 2 1 Link to comment Share on other sites More sharing options...
breezer Posted November 21, 2018 Author Share Posted November 21, 2018 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. 2 Link to comment Share on other sites More sharing options...
bernhard Posted November 22, 2018 Share Posted November 22, 2018 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. 1 Link to comment Share on other sites More sharing options...
breezer Posted November 22, 2018 Author Share Posted November 22, 2018 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(); Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now