Jim Bailie Posted March 27, 2018 Posted March 27, 2018 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!
elabx Posted March 27, 2018 Posted March 27, 2018 You can check for AJAX calls with: $config->ajax Sets to true if request is async. 3
Jim Bailie Posted March 27, 2018 Author Posted March 27, 2018 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.
kongondo Posted March 27, 2018 Posted March 27, 2018 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 4
horst Posted March 28, 2018 Posted March 28, 2018 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 4
dragan Posted March 28, 2018 Posted March 28, 2018 Perhaps you can find some inspiration / ideas from https://github.com/tutsplus/how-to-create-an-ajax-driven-theme-for-processwire 3
Jim Bailie Posted March 28, 2018 Author Posted March 28, 2018 @horst That's it! Thank you. @dragan Great tip. Will install an instance and see what's going on. Thanks all 1
Macrura Posted March 28, 2018 Posted March 28, 2018 you can also just return $this->halt() at the end of your ajax template... (similar to exit as @kongondo posted but possibly a tad more elegant ( https://processwire.com/blog/posts/processwire-2.6.8-brings-new-version-of-reno-admin-theme-and-more/#new-this-gt-halt-method-for-use-in-template-files ) 4
Jim Bailie Posted March 29, 2018 Author Posted March 29, 2018 @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!
horst Posted March 29, 2018 Posted March 29, 2018 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; } 3
Jim Bailie Posted March 29, 2018 Author Posted March 29, 2018 @horst You're like the gift that keeps giving. Now I know I can call a region directly! Nice... 1
horst Posted March 30, 2018 Posted March 30, 2018 (edited) 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 March 30, 2018 by horst added google search example 1
neosin Posted March 30, 2018 Posted March 30, 2018 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" ));
Jim Bailie Posted April 3, 2018 Author Posted April 3, 2018 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 1
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