Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Biology Forums on August 09, 2018, 10:47:40 AM

Title: AJAX-related inquiry
Post by: Biology Forums on August 09, 2018, 10:47:40 AM
Is it possible to dynamically load a function within a template file without loading the header and footer?

Assuming that the URL loads:

website.com/index.php?action=xyz

I've managed to work it by using exit(); at the very end, but in case there's a better method, I'd like to know.
Title: Re: AJAX-related inquiry
Post by: Arantor on August 09, 2018, 11:26:24 AM
Set $context['template_layers'] to an empty array to skip them.
Title: Re: AJAX-related inquiry
Post by: Biology Forums on August 09, 2018, 08:14:16 PM
On a similar note, how do I stop Google from crawling these AJAX-based pages?
Title: Re: AJAX-related inquiry
Post by: Arantor on August 10, 2018, 02:59:00 AM
Don't link to them in the first place?

Failing that there is a directive you can set in $context to emit a no index, but I don't remember what it is offhand.
Title: Re: AJAX-related inquiry
Post by: Biology Forums on August 10, 2018, 10:42:53 AM
Quote from: Arantor on August 10, 2018, 02:59:00 AM
Don't link to them in the first place?

Failing that there is a directive you can set in $context to emit a no index, but I don't remember what it is offhand.

It's hard not to, and even if I don't via some javascript code, search engines still get to them
Title: Re: AJAX-related inquiry
Post by: Biology Forums on August 16, 2018, 11:09:33 AM
One more question based on my original inquery, I tried this:

function search()
{
global $context;

$context['template_layers'] = array();

echo'My Text';
}


It comes up as a white screen with "my text" then underneath: An Error Has Occurred! Unable to load the 'main' template.

What does that suggest?
Title: Re: AJAX-related inquiry
Post by: Arantor on August 16, 2018, 11:55:24 AM
That suggests you haven't set up $context['sub_template'] to point to the template_ function that should be handling your output because separating out the logic of processing from its output is great.

If you need to spit out JSON and no other stuff, echo the json_encode then call obExit(false) to send without any other processing.
Title: Re: AJAX-related inquiry
Post by: Biology Forums on August 16, 2018, 12:09:15 PM
Thank you