Jump to content

Ajax - Send raw template only, without _main.php


Jim Bailie
 Share

Recommended Posts

Hello,

After doing a deep dive into Processwire over the weekend (better late than never), I'm starting to layout an app to receive a migration of an existing app. I'm sure I'll have several questions, but here's my first:

Question: I'm using the _main.php/regions template approach. However, I need to do some ajax calls for some html. How would I tell a page just to include only the needed template/fields?

I can probably figure this out, but in the interest of time...Thanks!

Link to comment
Share on other sites

2 hours ago, elabx said:

You can check for AJAX calls with:


$config->ajax

Sets to true if request is async.

Hmmm. That's good to know, but is my problem solved merely by being an ajax request? I suppose I could just set everything up and check it out.

Link to comment
Share on other sites

HI @Jim Bailie.

Welcome to ProcessWire and the forums.

 

3 hours ago, Jim Bailie said:

How would I tell a page just to include only the needed template/fields?

I don't quite understand the question. ProcessWire does not output anything in the frontend unless you tell it to. Maybe if you could expound a little bit on your question? Otherwise, it's as simple as:

if('your condition here') {
 // do this
}

else {
 // do that
}

As for Ajax..just to elaborate on @elabx's answer

if($config->ajax) {
 // ajax request sent; output ajaxy response stuff and exit;
 $data = array('foo', 'bar');
 header('Content-Type: application/json');
 echo json_encode($data);
 exit;// or use PW halt() if it suits your needs
}

// echo normal non-ajax stuff

 

  • Like 4
Link to comment
Share on other sites

You can create a separate template for the ajax page (a single page URL, that is called for all ajax requests) and check both to be disabled, prepend and append files in the template settings. 

Under templates / files:

deactivate auto prepend of _main.php

 

IMG_4486.thumb.PNG.ee7d3ba6e7891a2c0b82e6b7cf81c783.PNG

IMG_4484.thumb.PNG.4ad369ef2ec47ff84f41dee7d93fd466.PNG

 

  • Like 4
Link to comment
Share on other sites

@Macrura You know, I hadn't given much thought to that exit; but after reading up on the halt method, I then found out that _main.php is the last to load. So yeah, this approach could be used to consolidate data/display functionality on a single page. Thanks!

Link to comment
Share on other sites

That's ProcessWire! A series of ways to reach your goal. Pick your favourite: Do you like uphill climbing, go there. Do you like to stroll, go here. :)

Another way would be to put a snippet like this into the top of your _main.php:

<?php namespace ProcessWire;

if('ajax' == $page->template || $config->ajax) {
    echo region('main');
    return;
}

:lol:

  • Like 3
Link to comment
Share on other sites

12 hours ago, Jim Bailie said:

@horst You're like the gift that keeps giving. Now I know I can call a region directly! Nice...

To enable this, depending on your PW-Version, you may need to set the $config var useFunctionsAPI to true:

$config->useFunctionsAPI = true;  // pages()->find(), sanitizer()->pageName(), region('content', $markup),

Links:

https://processwire.com/blog/posts/processwire-3.0.40-core-updates/

https://processwire.com/blog/posts/processwire-3.0.39-core-updates/#new-functions-api

https://processwire.com/blog/posts/processwire-3.0.39-core-updates/#new-region-function

 

PS:

To easily find things on the PW site, you may use g**gles search like this:

site:processwire.com $config->useFunctionsAPI

Edited by horst
added google search example
  • Like 1
Link to comment
Share on other sites

On 3/27/2018 at 12:37 PM, Jim Bailie said:

Ajax - Send raw template only, without _main.php

If I understand correctly, you want to send data from a template without using the _main file.

To send data without using _main.php you can set this in the template itself.

Open the template in PW and goto the "Files" tab and at the bottom right side you see "Append File" and under that is a checkbox which says "Disable automatic append of file: _main.php". Make sure to have that box checked.

Now in your actual template file (example: ajax_out.php) you would put the header you want to use and just output as usual.

header("Content-type: application/json");
echo json_encode(array(
    "title" => $page->title,
    "message" => $page->body,
    "something" => "Lorem ipsum"
));

 

Link to comment
Share on other sites

Finally back in the saddle:

@horst Thanks for these links! Even though it's in the new skyscraper code, I hadn't even noticed the region method initially, so these are valuable links for anyone diving into pw/ajax in the future.

@neosin Thanks! Yeah, you're exactly right. Horst also pointed this out earlier in the thread...but it was in German :)

  • Like 1
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...