Jim Bailie

Ajax - Send raw template only, without _main.php

Recommended Posts


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!

Share this post

Link to post
Share on other sites

You can check for AJAX calls with:


Sets to true if request is async.

  • Like 3

Share this post

Link to post
Share on other sites
2 hours ago, elabx said:

You can check for AJAX calls with:


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.

Share this post

Link to post
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

Share this post

Link to post
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





  • Like 4

Share this post

Link to post
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!

Share this post

Link to post
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');


  • Like 3

Share this post

Link to post
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),







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

Share this post

Link to post
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"


Share this post

Link to post
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

Share this post

Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By gerald
      In my frontend I would like to implement a small AJAX-solution, but it does´nt work. For example:
      template/js/ajax.js: contains jquery-ajax-snippet with click-function like this: $.post('ajax.inc', function(e) {});
      template/ajax.inc: contains db-query with HTML-output
      template/home.php: contains div-element for the ajax-response, e.g. <div id='result'>...ajax-response...</div>
      It seems, PW does´nt allow database-requests via AJAX (jquery). I know the post from Ryan "How to work with AJAX driven content in ProcessWire". But this solution requires a completely new template-concept. I only need a simple solution for a small ajax-db-request.
      Thanks to all
    • By Robin S
      This module corrects a few things that I find awkward about the "Add New Template" workflow in the PW admin. I opened a wishlist topic a while back because it would good to resolve some of these things in the core, but this module is a stopgap for now.
      Originally I was going to share these as a few standalone hooks, but decided to bundle them together in a configurable module instead.
      Add Template Enhancements
      A module for ProcessWire CMS/CMF. Adds some efficiency enhancements when adding or cloning templates via admin.

      Derive label from name when new template added: if you like to give each of your templates a label then this feature can save some time. The label can be added automatically when templates are added in admin, in admin/API, or not at all. There are options for underscore/hyphen replacement and capitalisation of the label. Edit template after add: when adding only a single template, the template is automatically opened for editing after it is added. Copy field contexts when cloning: this copies the field contexts (a.k.a. overrides such as column width, label and description) from the source template to the new template when using the "Duplicate/clone this template?" feature on the Advanced tab. Copy field contexts when duplicating fields: this copies the field contexts if you select the "Duplicate fields used by another template" option when adding a new template. Usage
      Install the Add Template Enhancements module.
      Configure the module settings according to what suits you.
    • By joe_ma
      On a new pw 3.0.89 installation of mine I've got a problem with role permissions. The setup is like this:
      Templates: home, basic-page, text-only, folder
      All of these have no restrictions in the "family" section and all of them have the same access definitions (see edit-template.jpg below)
      There is a role "redaktor" that has permission to edit, add, delete, move and also clone pages (see permissions.jpg below).
      Now, when logged in as a user with this role, I can only choose from basic-page and folder. The text-only template is not available.

    • By louisstephens
      So I was going to build a todo tracking app for myself to test/broaden my knowledge of processwire, and so far it has been taxing 😓.  My Site structure is:
      - Project One - Phase One - Task - Task - Task - Phase Two - Task - Task - Task So far it was pretty easy, I can easily foreach through the Project and get the phases with their tasks. However, it gets a bit muddled when I have more than one project as I was trying to have a dashboard where the content switches out to the selected project as opposed to accessing each project via their own url. How would yall handle this?
      My next hurdle is each task has a select field (for project status) that I want to update via ajax (for the smooth transitioning).
      Scenario: You finish a task, change the option from "new" to "completed", and an onclick changes the status drop down background to green (which I have working), but then posts/saves a field on the backend to the new option.
      I have a page called "Actions" set up with url segments using
      if ($config->ajax && $input->urlSegment1 == 'change-status') { //save update field on admin } However, I am a little fuzzy on how to actually pass the current page along with the new selected status to actually update the page (task). I guess I am not very far into my endeavor. Any help would be greatly appreciated.
    • By EyeDentify
      I as have been a bit confused for some time about how the "Markup Regions" functionality in Processwire worked.
      But i have know read a bit more and think that i am getting to grips with it. And Markup Regions is going to be huge.

      To aid me in understanding Markup Regions better i started to read the Source code for the new "Regular" theme in conjunction with the Blog about the markup regions.
      It helped me a great deal to understand the basics and more fine details of it.

      A tip is to open both links and use the Source code of the "Regular" theme while reading the blog post.
      The Source code:
      The Blog post:
      I hope this could help others starting out with markup regions.
      Just take it slow and read it a couple a times and soon you will see the greatness of markup regions.